방법: GenericPrincipal 및 GenericIdentity 개체 만들기
GenericPrincipal 클래스와 함께 GenericIdentity 클래스를 사용하여 Windows 도메인과 독립적으로 존재하는 권한 부여 체계를 만들 수 있습니다.
GenericPrincipal 개체를 만들려면
identity 클래스의 새 인스턴스를 만들고, 유지하고 싶은 이름으로 초기화합니다. 다음 코드에서는 새 GenericIdentity 개체를 만들고 이를
MyUser
라는 이름으로 초기화합니다.Dim myIdentity As New GenericIdentity("MyUser")
GenericIdentity myIdentity = new GenericIdentity("MyUser");
GenericPrincipal 클래스의 새 인스턴스를 만들고 이를 이전에 만든 GenericIdentity 개체 및 해당 Principal에 연결할 역할을 나타내는 문자열 배열로 초기화합니다. 다음의 코드 예제에서는 관리자 역할 및 사용자 역할을 나타내는 문자열 배열을 지정합니다. 이렇게 하면 앞의 GenericIdentity 및 문자열 배열을 사용하여 GenericPrincipal이 초기화됩니다.
Dim myStringArray As String() = {"Manager", "Teller"} DIm myPrincipal As New GenericPrincipal(myIdentity, myStringArray)
String[] myStringArray = {"Manager", "Teller"}; GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myStringArray);
해당 Principal을 현재 스레드에 연결하려면 다음 코드를 사용합니다. 이는 주체의 유효성을 여러 번 검사해야 하거나, 애플리케이션에서 실행되는 다른 코드로 유효성을 검사해야 하거나, PrincipalPermission 개체로 유효성을 검사해야 하는 상황에서 유용합니다. Principal 개체를 스레드에 연결하지 않고도 이 개체에 대해 역할 기반 확인을 수행할 수 있습니다. 자세한 내용은 Principal 개체 바꾸기를 참조하세요.
Thread.CurrentPrincipal = myPrincipal
Thread.CurrentPrincipal = myPrincipal;
예시
다음 코드 예제에서는 GenericPrincipal 및 GenericIdentity의 인스턴스를 만드는 방법을 보여 줍니다 이 코드에서는 해당 개체의 값을 콘솔에 표시합니다.
Imports System.Security.Principal
Imports System.Threading
Public Class Class1
Public Shared Sub Main()
' Create generic identity.
Dim myIdentity As New GenericIdentity("MyIdentity")
' Create generic principal.
Dim myStringArray As String() = {"Manager", "Teller"}
Dim myPrincipal As New GenericPrincipal(myIdentity, myStringArray)
' Attach the principal to the current thread.
' This is not required unless repeated validation must occur,
' other code in your application must validate, or the
' PrincipalPermission object is used.
Thread.CurrentPrincipal = myPrincipal
' Print values to the console.
Dim name As String = myPrincipal.Identity.Name
Dim auth As Boolean = myPrincipal.Identity.IsAuthenticated
Dim isInRole As Boolean = myPrincipal.IsInRole("Manager")
Console.WriteLine("The name is: {0}", name)
Console.WriteLine("The isAuthenticated is: {0}", auth)
Console.WriteLine("Is this a Manager? {0}", isInRole)
End Sub
End Class
using System;
using System.Security.Principal;
using System.Threading;
public class Class1
{
public static int Main(string[] args)
{
// Create generic identity.
GenericIdentity myIdentity = new GenericIdentity("MyIdentity");
// Create generic principal.
String[] myStringArray = {"Manager", "Teller"};
GenericPrincipal myPrincipal =
new GenericPrincipal(myIdentity, myStringArray);
// Attach the principal to the current thread.
// This is not required unless repeated validation must occur,
// other code in your application must validate, or the
// PrincipalPermission object is used.
Thread.CurrentPrincipal = myPrincipal;
// Print values to the console.
String name = myPrincipal.Identity.Name;
bool auth = myPrincipal.Identity.IsAuthenticated;
bool isInRole = myPrincipal.IsInRole("Manager");
Console.WriteLine("The name is: {0}", name);
Console.WriteLine("The isAuthenticated is: {0}", auth);
Console.WriteLine("Is this a Manager? {0}", isInRole);
return 0;
}
}
애플리케이션을 실행하면 다음과 같은 결과가 표시됩니다.
The Name is: MyIdentity
The IsAuthenticated is: True
Is this a Manager? True
참고 항목
.NET