Clase AuthorizationAttribute
Actúa como la clase base para las clases que se usan para controlar las autorizaciones mediante metadatos personalizados.
Espacio de nombres: System.ComponentModel.DataAnnotations
Ensamblado: System.ServiceModel.DomainServices.Server (en system.servicemodel.domainservices.server.dll)
Uso
'Uso
Dim instance As AuthorizationAttribute
Sintaxis
'Declaración
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
Ejemplo
En el siguiente ejemplo, se muestra una implementación de la clase 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.");
}
}
}
Notas
Puede crear una clase que se derive de la clase AuthorizationAttribute con el fin de implementar una directiva de autorización personalizada. Cuando crea una clase derivada, es preciso que implemente la lógica de autorización en el método IsAuthorized. El método IsAuthorized incluye parámetros para un objeto de IPrincipal y un objeto de AuthorizationContext. Puede utilizar estos parámetros para determinar si un usuario dispone de autorización. En la clase derivada, puede agregar propiedades que se especificaron en la declaración de atributos y se usaron en la lógica de autorización. Puede aplicar el atributo a operaciones que necesiten la directiva de autorización personalizada.
Jerarquía de herencia
System.Object
System.Attribute
System.ComponentModel.DataAnnotations.AuthorizationAttribute
System.ServiceModel.DomainServices.Server.RequiresAuthenticationAttribute
System.ServiceModel.DomainServices.Server.RequiresRoleAttribute
Seguridad para subprocesos
Cualquier miembro estático público (Compartidos en Visual Basic) de este tipo es seguro para subprocesos. No se garantiza que los miembros de instancia sean seguros para subprocesos.
Plataformas
Plataformas de desarrollo
Microsoft Windows XP Home Edition, Windows XP Professional, Windows Server 2003 , Windows Server 2008 y Windows 2000
Plataformas de destino
Change History
Vea también
Referencia
Miembros AuthorizationAttribute
Espacio de nombres System.ComponentModel.DataAnnotations