Principal 개체 바꾸기
인증 서비스를 제공하는 애플리케이션은 지정된 스레드에 대해 Principal 개체(IPrincipal)를 대체할 수 있어야 합니다. 또한 보안 시스템은 Principal 개체를 대체하는 기능을 보호할 수 있어야 합니다. 악의적으로 연결된 잘못된 Principal 은 허위 ID 또는 역할을 요청함으로써 애플리케이션의 보안을 손상시킵니다. 따라서 Principal 개체를 대체할 수 있는 기능이 필요한 애플리케이션에서는 보안 주체 컨트롤에 대한 System.Security.Permissions.SecurityPermission 개체가 허가되어야 합니다. (이 사용 권한은 역할을 기반으로 하는 보안 검사를 수행하거나 Principal 개체를 만들 때는 필요하지 않습니다.)
현재 Principal 개체는 다음과 같은 작업을 수행하여 대체할 수 있습니다.
대체 Principal 개체 및 연결된 Identity 개체를 만듭니다.
새 Principal 개체를 호출 컨텍스트에 연결합니다.
예시
다음 예제에서는 일반적인 보안 주체 개체를 만들고 이 개체를 사용하여 스레드의 보안 주체를 설정하는 방법을 보여줍니다.
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;
}
}
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
End Class
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET