Principal 개체 바꾸기
인증 서비스를 제공하는 응용 프로그램에서는 지정된 스레드에 대한 Principal 개체를 바꿀 수 있어야 합니다. 또한, 악의를 갖고 틀린 Principal을 사용하면 올바르지 않은 ID 또는 역할이 요구되어 응용 프로그램의 보안이 손상되기 때문에 보안 시스템에서는 Principal 개체를 바꾸는 기능이 보호되어야 합니다. 따라서 Principal 개체를 바꾸는 기능을 필요로 하는 응용 프로그램에서는 보안 주체 제어를 위한 System.Security.Permissions.SecurityPermission 개체가 부여되어야 합니다. 그러나 역할 기반 보안 검사를 수행하거나 Principal 개체를 만들 때에는 이 사용 권한이 필요하지 않습니다.
다음과 같은 작업을 수행하여 현재 Principal 개체를 바꿀 수 있습니다.
대체 Principal 개체 및 관련된 Identity 개체를 만듭니다.
새 Principal 개체를 호출 컨텍스트에 연결합니다.
다음 예제에서는 제네릭 Principal 개체를 만들고 이를 사용하여 스레드의 보안 주체를 설정하는 방법을 보여 줍니다.
Imports System
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal
Class SecurityPrincipalDemo
' Create a generic principal based on values from the current
' WindowsIdentity.
Private Shared Function GetGenericPrincipal() As GenericPrincipal
' Use values from the current WindowsIdentity to construct
' a set of GenericPrincipal roles.
Dim windowsIdentity As WindowsIdentity = windowsIdentity.GetCurrent()
Dim roles(9) As String
If windowsIdentity.IsAuthenticated Then
' Add custom NetworkUser role.
roles(0) = "NetworkUser"
End If
If windowsIdentity.IsGuest Then
' Add custom GuestUser role.
roles(1) = "GuestUser"
End If
If windowsIdentity.IsSystem Then
' Add custom SystemUser role.
roles(2) = "SystemUser"
End If
' Construct a GenericIdentity object based on the current Windows
' identity name and authentication type.
Dim authenticationType As String = windowsIdentity.AuthenticationType
Dim userName As String = windowsIdentity.Name
Dim genericIdentity As New GenericIdentity(userName, authenticationType)
' Construct a GenericPrincipal object based on the generic identity
' and custom roles for the user.
Dim genericPrincipal As New GenericPrincipal(genericIdentity, roles)
Return genericPrincipal
End Function 'GetGenericPrincipal
Public Shared Sub Main()
' Retrieve a GenericPrincipal that is based on the current user's
' WindowsIdentity.
Dim genericPrincipal As GenericPrincipal = GetGenericPrincipal()
' Retrieve the generic identity of the GenericPrincipal object.
Dim principalIdentity As GenericIdentity = CType(genericPrincipal.Identity, GenericIdentity)
' Display the identity name and authentication type.
If principalIdentity.IsAuthenticated Then
Console.WriteLine(principalIdentity.Name)
Console.WriteLine("Type:" + principalIdentity.AuthenticationType)
End If
' Verify that the generic principal has been assigned the
' NetworkUser role.
If genericPrincipal.IsInRole("NetworkUser") Then
Console.WriteLine("User belongs to the NetworkUser role.")
End If
Thread.CurrentPrincipal = genericPrincipal
End Sub 'Main
End Class 'SecurityPrincipalDemo
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
class SecurityPrincipalDemo
{
public static void Main()
{
// Retrieve a GenericPrincipal that is based on the current user's
// WindowsIdentity.
GenericPrincipal genericPrincipal = GetGenericPrincipal();
// Retrieve the generic identity of the GenericPrincipal object.
GenericIdentity principalIdentity =
(GenericIdentity)genericPrincipal.Identity;
// Display the identity name and authentication type.
if (principalIdentity.IsAuthenticated)
{
Console.WriteLine(principalIdentity.Name);
Console.WriteLine("Type:" + principalIdentity.AuthenticationType);
}
// Verify that the generic principal has been assigned the
// NetworkUser role.
if (genericPrincipal.IsInRole("NetworkUser"))
{
Console.WriteLine("User belongs to the NetworkUser role.");
}
Thread.CurrentPrincipal = genericPrincipal;
}
// Create a generic principal based on values from the current
// WindowsIdentity.
private static GenericPrincipal GetGenericPrincipal()
{
// Use values from the current WindowsIdentity to construct
// a set of GenericPrincipal roles.
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string[] roles = new string[10];
if (windowsIdentity.IsAuthenticated)
{
// Add custom NetworkUser role.
roles[0] = "NetworkUser";
}
if (windowsIdentity.IsGuest)
{
// Add custom GuestUser role.
roles[1] = "GuestUser";
}
if (windowsIdentity.IsSystem)
{
// Add custom SystemUser role.
roles[2] = "SystemUser";
}
// Construct a GenericIdentity object based on the current Windows
// identity name and authentication type.
string authenticationType = windowsIdentity.AuthenticationType;
string userName = windowsIdentity.Name;
GenericIdentity genericIdentity =
new GenericIdentity(userName, authenticationType);
// Construct a GenericPrincipal object based on the generic identity
// and custom roles for the user.
GenericPrincipal genericPrincipal =
new GenericPrincipal(genericIdentity, roles);
return genericPrincipal;
}
}