撤消授予用户访问数据库的权限

时间:2015-04-09 13:41:06

标签: c# sql database ssms

我正在构建一个Web应用程序,可以管理多个数据库的用户访问。我使用以下代码为用户提供了对数据库的读取权限:

    public void giveUserReadRightsForDatabase(int serverID, string dbName, string dbUsername, string myPassword)
    {
        ISFramework.Database myDB = new ISFramework.Database();

        string myQuery = "IF NOT EXISTS (SELECT name FROM sys.sql_logins WHERE name ='" + dbUsername + "')" + Environment.NewLine +
                            "BEGIN" + Environment.NewLine +
                            "CREATE LOGIN " + dbUsername + " WITH PASSWORD = '" + myPassword +
 "', CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF" + Environment.NewLine +
                            "END" + Environment.NewLine +

                            "BEGIN TRY" + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "CREATE USER " + dbUsername + " FOR LOGIN " + dbUsername + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "ALTER ROLE db_datareader ADD MEMBER " + dbUsername + Environment.NewLine +

                            "END TRY" + Environment.NewLine +
                            "BEGIN CATCH" + Environment.NewLine +
                            "END CATCH";

        myDB.executeNonQuery("DBServer" + serverID, myQuery);
    }

我使用以下代码删除权限:

    public void removePermissionsForThisDatabase(int serverID, string dbUsername, string dbName)
    {
        ISFramework.Database myDB = new ISFramework.Database();

        string myQuery = "BEGIN TRY" + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "CREATE USER " + dbUsername + " FOR LOGIN " + dbUsername + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "ALTER ROLE db_datareader DROP MEMBER " + dbUsername + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "ALTER ROLE db_datawriter DROP MEMBER " + dbUsername + Environment.NewLine +
                            "USE [" + dbName + "]" + Environment.NewLine +
                            "IF  EXISTS (SELECT * FROM sys.sql_logins WHERE name = N'" + dbUsername + "')" + Environment.NewLine +
                            "DROP USER " + dbUsername + Environment.NewLine +
                            "IF  EXISTS (SELECT * FROM sys.database_principals WHERE name = N'" + dbUsername + "')" + Environment.NewLine +
                            "DROP LOGIN " + dbUsername + Environment.NewLine +
                            "END TRY" + Environment.NewLine +
                            "BEGIN CATCH" + Environment.NewLine +
                            "END CATCH";

        myDB.executeNonQuery("DBServer" + serverID, myQuery);
    }

在第一种方法结束时,用户可以访问和读取数据库,这很好。虽然当我使用第二种方法时,用户仍然可以访问数据库,但这并不好。我已经调试并确认程序正在执行方法并执行查询,因此查询本身必须有错误。

我已经尝试在SQL Server Management Studio中执行以下操作,这确实可以完成它的工作,所以我真的没有任何线索,说实话。永远不会出现错误消息,但代码并不是应该如此。

 USE [Dev_ISFramework]
 IF  EXISTS (SELECT * FROM sys.sql_logins WHERE name = N'Johnny456')
 DROP USER Johnny456

1 个答案:

答案 0 :(得分:2)

我不是这里的专家,但是如果您的第二种方法在SQL的开头使用BEGIN TRY,那么尝试在用户存在时创建一个引发静默捕获的异常。所以你的角色变更没有被执行。

您必须重新访问removePermissionsForThisDatabase()中使用的整个SQL代码才能执行您想要的操作。