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
範例
下列範例示範 AuthorizationAttribute 的實作,該實作會使用 AuthorizationContext 值來自訂驗證。
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 欄位來表示成功的授權要求。當這個類別的任何執行個體不是 null 或 Allowed 時會表示已經拒絕的授權要求。
當這個類別的執行個體包含 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 命名空間