My (C#) application runs, connecting to an SQL server database with an Application Role.
However, I need it to run a DBCC CHECKDB
, but I get an exception:
User 'MyAppRole' does not have permission to run DBCC checkdb for database 'MyDatabase'.
I found out that to run this command, I can't just grant permission to the Application Role to perform this action, I need to be a member of the db_owner
role.
Things I've Tried:
1: In SQL Management Studio, ticking the db_owner box in the properties of MyAppRole for this database. However I get the same exception.
2: Add db_owner role.
exec sp_addrolemember 'db_owner', 'MyAppRole'
But now when I run the application I get this exception at the point of doing the DBCC CHECKDB:
The server principal "MYDOMAIN\MyWindowsUsername" is not able to access the database "MyDatabase" under the current security context.
which is especially confusing because in SQL Managment Studio, I can easily run the command, after logging in using Windows Authentication.
3: creating a stored procedure with execute as Owner
CREATE PROCEDURE sp_dbcccheck WITH EXECUTE AS OWNER
AS
BEGIN
DBCC CHECKDB (MyDatabase) WITH TABLERESULTS
END
GO
... and given my MyAppRole
execute Permission. But I still get the exception:
The server principal "MYDOMAIN\MyWindowsUsername" is not able to access the database "MyDatabase" under the current security context.
...which I also get if I try to execute this stored procedure from SQL Management Studio.
4: Give Application Role "CONTROL" permission (some of these would seem to defeat the purpose of an application role but anyway, grasping at straws)
GRANT CONTROL ON DATABASE::MyDatabase TO MyAppRole
but this results in the original error when the application tries to run the check.
User 'MyAppRole' does not have permission to run DBCC checkdb for database 'MyDatabase'
How do I correctly give my application role permission to do this check?