How to: Create a Custom Authorization Attribute
[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]
This topic demonstrates how to add a custom attribute for authorization. The WCF RIA Services framework provides the RequiresAuthenticationAttribute and RequiresRoleAttribute attributes. These attributes enable you to easily specify which domain operations are only available to authenticated users or users in a specific role. In addition to these two attributes, you can create an attribute that represents customized authorization logic and then apply the attribute to domain operations.
When you expose a domain service, the domain service is available to everyone on the network. You cannot assume that your client application is the only application that will access the domain service. You can use customized authentication attributes to restrict access to domain operations even when the domain operation is accessed outside of your client application.
In this topic you create a custom authorization attribute by creating a class that derives from AuthorizationAttribute and overriding the IsAuthorized method to provide your customized logic. You can use the IPrincipal parameter and the AuthorizationContext parameter to access information that may be required within your customized authentication code. The AuthorizationContext object is null on query operations.
To create a customized authorization attribute
In the server project, create a class that derives from AuthorizationAttribute.
Override the IsAuthorized method and add logic for determining authorization.
The following example shows a custom attribute named RestrictAccessToAssignedManagers that checks whether the authenticated user is the manager of the employee whose EmployeePayHistory record is being modified.
Public Class CheckAttendeeNameAttribute Inherits System.Web.DomainServices.AuthorizationAttribute Public Overrides Function Authorize(ByVal principal As System.Security.Principal.IPrincipal) As Boolean If (principal.IsInRole("Attendee") And principal.Identity.Name.StartsWith("A")) Then Return True Else Return False End If End Function End Class
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 CheckAttendeeNameAttribute : System.Web.DomainServices.AuthorizationAttribute { public override bool Authorize(System.Security.Principal.IPrincipal principal) { if (principal.IsInRole("Attendee") && principal.Identity.Name.StartsWith("A")) { return true; } else { return false; } } }
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."); } } }
To perform the customized authorization logic, apply the custom authorization attribute to the domain operation.
The following example shows the RestrictAccessToAssignedManagers attribute applied to a domain operation.
<RestrictAccessToAssignedManagers()> _ Public Sub InsertEmployeePayHistory(ByVal employeePayHistory As EmployeePayHistory) If ((employeePayHistory.EntityState = EntityState.Detached) _ = False) Then Me.ObjectContext.ObjectStateManager.ChangeObjectState(employeePayHistory, EntityState.Added) Else Me.ObjectContext.EmployeePayHistories.AddObject(employeePayHistory) End If End Sub
[RestrictAccessToAssignedManagers] public void InsertEmployeePayHistory(EmployeePayHistory employeePayHistory) { if ((employeePayHistory.EntityState != EntityState.Detached)) { this.ObjectContext.ObjectStateManager.ChangeObjectState(employeePayHistory, EntityState.Added); } else { this.ObjectContext.EmployeePayHistories.AddObject(employeePayHistory); } }