Share via


권한 부여, 취소 및 거부

ServerPermission 개체는 일련의 권한 또는 개별 서버 권한을 ServerPermissionSet 개체에 할당하는 데 사용됩니다. 서버 수준 권한의 경우 피부여자가 로그온을 참조합니다. Windows에 의해 인증된 로그온이 Windows 사용자 이름으로 나열됩니다. 이 코드 예제를 실행하면 피부여자로부터 권한이 취소되고 EnumServerPermissions 메서드로 권한이 제거되었는지 확인합니다.

DatabasePermissionSet 개체 및 ObjectPermissionSet 개체를 사용하여 비슷한 방법으로 데이터베이스 권한 및 데이터베이스 개체 권한을 할당할 수 있습니다.

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 방법: Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기 또는 방법: Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하십시오.

Visual Basic에서 서버 권한 부여

이 코드 예제는 지정된 로그인에 Create Endpoint 및 Alter Any Endpoint 권한을 부여한 다음, 권한을 열거하고 표시합니다. 권한 중 하나를 취소하고 나서 권한을 다시 열거합니다. 이 예에서는 지정된 로그인에 지정된 시작 권한이 있다고 가정합니다.

'Connect to the local, default instance of SQL Server.
Dim svr As Server
svr = New Server()
'Define a ServerPermissionSet that contains permission to Create Endpoint and Alter Any Endpoint.
Dim sps As ServerPermissionSet
sps = New ServerPermissionSet(ServerPermission.CreateEndpoint)
sps.Add(ServerPermission.AlterAnyEndpoint)
'This sample assumes that the grantee already has permission to Create Endpoints. 
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
Dim spis As ServerPermissionInfo()
spis = svr.EnumServerPermissions(vGrantee, sps)
Dim spi As ServerPermissionInfo
Console.WriteLine("=================Before revoke===========================")
For Each spi In spis
    Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine(" ")
'Remove a permission from the set.
sps.Remove(ServerPermission.CreateEndpoint)
'Revoke the create endpoint permission from the grantee.
svr.Revoke(sps, vGrantee)
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps)
Console.WriteLine("=================After revoke============================")
For Each spi In spis
    Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine(" ")
'Grant the Create Endpoint permission to the grantee.
svr.Grant(sps, vGrantee)
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps)
Console.WriteLine("=================After grant=============================")
For Each spi In spis
    Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine("")

Visual C#에서 서버 권한 부여

이 코드 예제는 지정된 로그인에 Create Endpoint 및 Alter Any Endpoint 권한을 부여한 다음, 권한을 열거하고 표시합니다. 권한 중 하나를 취소하고 나서 권한을 다시 열거합니다. 이 예에서는 지정된 로그인에 지정된 시작 권한이 있다고 가정합니다.

//Connect to the local, default instance of SQL Server. 
{ 
Server svr = default(Server); 
svr = new Server(); 
//Define a ServerPermissionSet that contains permission to Create Endpoint and Alter Any Endpoint. 
ServerPermissionSet sps = default(ServerPermissionSet); 
sps = new ServerPermissionSet(ServerPermission.CreateEndpoint); 
sps.Add(ServerPermission.AlterAnyEndpoint); 
//This sample assumes that the grantee already has permission to Create Endpoints. 
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable. 
ServerPermissionInfo[] spis = null; 
spis = svr.EnumServerPermissions(vGrantee, sps); 
ServerPermissionInfo spi = default(ServerPermissionInfo); 
Console.WriteLine("===========Before revoke=================="); 
foreach ( spi in spis) { 
    Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission."); 
} 
Console.WriteLine(" "); 
//Remove a permission from the set. 
sps.Remove(ServerPermission.CreateEndpoint); 
//Revoke the create endpoint permission from the grantee. 
svr.Revoke(sps, vGrantee); 
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable. 
spis = svr.EnumServerPermissions(vGrantee, sps); 
Console.WriteLine("=========After revoke================"); 
foreach ( spi in spis) { 
    Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission."); 
} 
Console.WriteLine(" "); 
//Grant the Create Endpoint permission to the grantee. 
svr.Grant(sps, vGrantee); 
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable. 
spis = svr.EnumServerPermissions(vGrantee, sps); 
Console.WriteLine("=========After grant==============="); 
foreach ( spi in spis) { 
    Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission."); 
} 
Console.WriteLine(""); 
}