Freigeben über


AuthorizationAttribute-Klasse

Dient als Basisklasse für Klassen, mit denen Autorisierungen über benutzerdefinierte Metadaten gesteuert werden.

Namespace: System.ComponentModel.DataAnnotations
Assembly: System.ServiceModel.DomainServices.Server (in system.servicemodel.domainservices.server.dll)

Verwendung

'Usage
Dim instance As AuthorizationAttribute

Syntax

'Declaration
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

Beispiel

Im folgenden Beispiel wird eine Implementierung der AuthorizationAttribute-Klasse veranschaulicht.

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.");
        }
    }
}

Hinweise

Sie erstellen eine Klasse, die von der AuthorizationAttribute-Klasse abgeleitet wird, um eine benutzerdefinierte Autorisierungsrichtlinie zu implementieren. Wenn Sie eine abgeleitete Klasse erstellen, müssen Sie die Autorisierungslogik in der IsAuthorized-Methode implementieren. Die IsAuthorized-Methode enthält Parameter für ein IPrincipal-Objekt und für ein AuthorizationContext-Objekt. Anhand dieser Parameter können Sie bestimmen, ob ein Benutzer autorisiert ist. In der abgeleiteten Klasse können Sie Eigenschaften hinzufügen, die in der Attributdeklaration angegeben und in der Autorisierungslogik verwendet werden. Wenden Sie das Attribut auf Vorgänge an, die die benutzerdefinierte Autorisierungsrichtlinie verwenden sollen.

Vererbungshierarchie

System.Object
   System.Attribute
    System.ComponentModel.DataAnnotations.AuthorizationAttribute
       System.ServiceModel.DomainServices.Server.RequiresAuthenticationAttribute
       System.ServiceModel.DomainServices.Server.RequiresRoleAttribute

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht sichergestellt.

Plattformen

Entwicklungsplattformen

Windows XP Home Edition, Windows XP Professional, Windows Server 2003 , Windows Server 2008 und Windows 2000

Zielplattformen

Change History

Siehe auch

Verweis

AuthorizationAttribute-Member
System.ComponentModel.DataAnnotations-Namespace