次の方法で共有


AuthorizationAttribute クラス

カスタム メタデータからの承認を制御するために使用されるクラスの基本クラスとして機能します。

名前空間: System.ComponentModel.DataAnnotations
アセンブリ: System.ServiceModel.DomainServices.Server (system.servicemodel.domainservices.server.dll 内)

使用方法

'使用
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 名前空間