AuthorizationContext 类

描述执行授权的上下文。

命名空间: System.ComponentModel.DataAnnotations
程序集: System.ServiceModel.DomainServices.Server(在 system.servicemodel.domainservices.server.dll 中)

用法

用法
Dim instance As AuthorizationContext

语法

声明
Public NotInheritable Class AuthorizationContext
    Implements IServiceProvider, IDisposable
public sealed class AuthorizationContext : IServiceProvider, IDisposable
public ref class AuthorizationContext sealed : IServiceProvider, IDisposable
public final class AuthorizationContext implements IServiceProvider, IDisposable
public final class AuthorizationContext implements IServiceProvider, IDisposable

示例

下面的示例演示如何实现通过使用 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.");
        }
    }
}

备注

此类包含用来描述相实例和正被授权的操作的信息。它可实现 IDisposable,在使用之后必须正确地释放。它支持 IServiceProvider,以便自定义验证代码可以获取其他可帮助其执行验证的服务。

可以访问 Items 属性包来获取有关授权的其他上下文信息。存储在 Items 中的值将对于使用此 AuthorizationContext 的授权方法可用。

此类还提供 IServiceContainer 实现以允许开发人员在运行时向上下文中添加服务。此容器可通过调用 GetService 方法并提供 IServiceContainer 的类型或者通过使用 ServiceContainer 属性来访问。

Instance 属性中的对象类型是操作中所涉及实体的类型。对于查询操作,Instance 属性为 null

继承层次结构

System.Object
  System.ComponentModel.DataAnnotations.AuthorizationContext

线程安全

此类型的任何公共静态(在 Visual Basic 中为 共享)成员都是线程安全的。不保证所有实例成员都是线程安全的。

平台

开发平台

Windows XP Home Edition, Windows XP Professional, Windows Server 2003 、Windows Server 2008 和 Windows 2000

目标平台

Change History

另请参见

参考

AuthorizationContext 成员
System.ComponentModel.DataAnnotations 命名空间