다음을 통해 공유


AuthorizationAttribute 클래스

사용자 지정 메타데이터를 통해 권한 부여를 제어하는 데 사용되는 클래스의 기본 클래스 역할을 합니다.

네임스페이스: System.ComponentModel.DataAnnotations
어셈블리: system.servicemodel.domainservices.server.dll의 System.ServiceModel.DomainServices.Server

사용법

‘사용 방법
Dim instance As AuthorizationAttribute

구문

‘선언
Public MustInherit Class AuthorizationAttribute
    Inherits Attribute
public abstract class AuthorizationAttribute : Attribute
public ref class AuthorizationAttribute abstract : public Attribute
public abstract class AuthorizationAttribute extends Attribute
public abstract class AuthorizationAttribute extends Attribute

예제

다음 예제에서는 AuthorizationAttribute 클래스 구현을 보여 줍니다.

Public Class RestrictAccessToAssignedManagers
    Inherits AuthorizationAttribute

    Protected Overrides Function IsAuthorized(ByVal principal As System.Security.Principal.IPrincipal, ByVal authorizationContext As System.ComponentModel.DataAnnotations.AuthorizationContext) As System.ComponentModel.DataAnnotations.AuthorizationResult
        Dim eph As EmployeePayHistory
        Dim selectedEmployee As Employee
        Dim authenticatedUser As Employee

        eph = CType(authorizationContext.Instance, EmployeePayHistory)
            
        Using context As New AdventureWorksEntities()
            selectedEmployee = context.Employees.SingleOrDefault(Function(e) e.EmployeeID = eph.EmployeeID)
            authenticatedUser = context.Employees.SingleOrDefault(Function(e) e.LoginID = principal.Identity.Name)
        End Using

        If (selectedEmployee.ManagerID = authenticatedUser.EmployeeID) Then
            Return AuthorizationResult.Allowed
        Else
            Return New AuthorizationResult("Only the authenticated manager for the employee can add a new record.")
        End If
    End Function
End Class
public class RestrictAccessToAssignedManagers : AuthorizationAttribute
{
    protected override AuthorizationResult IsAuthorized(System.Security.Principal.IPrincipal principal, AuthorizationContext authorizationContext)
    {
        EmployeePayHistory eph = (EmployeePayHistory)authorizationContext.Instance;
        Employee selectedEmployee;
        Employee authenticatedUser;

        using (AdventureWorksEntities context = new AdventureWorksEntities())
        {
            selectedEmployee = context.Employees.SingleOrDefault(e => e.EmployeeID == eph.EmployeeID);
            authenticatedUser = context.Employees.SingleOrDefault(e => e.LoginID == principal.Identity.Name);
        }

        if (selectedEmployee.ManagerID == authenticatedUser.EmployeeID)
        {
            return AuthorizationResult.Allowed;
        }
        else
        {
            return new AuthorizationResult("Only the authenticated manager for the employee can add a new record.");
        }
    }
}

설명

AuthorizationAttribute 클래스에서 파생되는 클래스를 만들어 사용자 지정 권한 부여 정책을 구현합니다. 파생 클래스를 만드는 경우 IsAuthorized 메서드에 권한 부여 논리를 구현해야 합니다. IsAuthorized 메서드에는 IPrincipal 개체와 AuthorizationContext 개체에 대한 매개 변수가 포함됩니다. 이러한 매개 변수를 사용하여 사용자가 권한이 있는지 확인할 수 있습니다. 파생 클래스에서 특성 선언에 지정되고 권한 부여 논리에 사용되는 속성을 추가할 수 있습니다. 사용자 지정 권한 부여 정책이 필요한 작업에 특성을 적용합니다.

상속 계층

System.Object
   System.Attribute
    System.ComponentModel.DataAnnotations.AuthorizationAttribute
       System.ServiceModel.DomainServices.Server.RequiresAuthenticationAttribute
       System.ServiceModel.DomainServices.Server.RequiresRoleAttribute

스레드 보안

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 모든 인스턴스 멤버는 스레드로부터 안전하지 않을 수 있습니다.

플랫폼

개발 플랫폼

Windows XP Home Edition, Windows XP Professional, Windows Server 2003 , Windows Server 2008 및 Windows 2000

대상 플랫폼

Change History

참고 항목

참조

AuthorizationAttribute 멤버
System.ComponentModel.DataAnnotations 네임스페이스