次の方法で共有


AuthorizationContext.Instance プロパティ

承認中のオブジェクト インスタンスを取得します。

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

使用方法

'使用
Dim instance As AuthorizationContext
Dim value As Object

value = instance.Instance

構文

'宣言
Public ReadOnly Property Instance As Object
public Object Instance { get; }
public:
property Object^ Instance {
    Object^ get ();
}
/** @property */
public Object get_Instance ()
public function get Instance () : Object

プロパティ値

承認中のオブジェクト インスタンス。

AuthorizationContext 値を使用して承認をカスタマイズする 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.");
        }
    }
}

解説

Instance プロパティを使用して、承認に関連したエンティティ オブジェクトを取得します。クエリを承認する場合や、操作の試行が可能かどうかを評価する場合などのように、使用可能なオブジェクト インスタンスがない場合、Instance プロパティは null になります。AuthorizationAttribute クラスから派生するクラスを作成した場合、カスタム承認プロセスを実装する際に Instance プロパティが null かどうかを確認してください。

スレッド セーフ

この型の public static (Visual Basic では Shared) メンバーは、スレッド セーフです。インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

プラットフォーム

開発プラットフォーム

Windows XP Home Edition, Windows XP Professional, Windows Server 2003 、Windows Server 2008、および Windows 2000

ターゲット プラットフォーム

Change History

参照

リファレンス

AuthorizationContext クラス
AuthorizationContext のメンバー
System.ComponentModel.DataAnnotations 名前空間