AuthorizationResult 类

包含授权请求的结果。

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

用法

用法
Dim instance As AuthorizationResult

语法

声明
Public NotInheritable Class AuthorizationResult
public sealed class AuthorizationResult
public ref class AuthorizationResult sealed
public final class AuthorizationResult
public final class AuthorizationResult

示例

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

备注

使用 Allowed 字段可以表示成功的授权请求。此类的、不为 nullAllowed 的实例表示已被拒绝的授权请求。

此类的、包含 null 的实例指示授权得到批准。您应当始终使用 Allowed 值,而不应当使用 null

继承层次结构

System.Object
  System.ComponentModel.DataAnnotations.AuthorizationResult

线程安全

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

平台

开发平台

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

目标平台

Change History

另请参见

参考

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