RequiresRoleAttribute Class

[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.]

Specifies a set of roles that are permitted to invoke a DomainOperationEntry.

Inheritance Hierarchy

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

Namespace: System.ServiceModel.DomainServices.Server
Assembly: System.ServiceModel.DomainServices.Server (in System.ServiceModel.DomainServices.Server.dll)

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field, AllowMultiple := True,  _
    Inherited := True)> _
Public NotInheritable Class RequiresRoleAttribute _
    Inherits AuthorizationAttribute
'Usage
Dim instance As RequiresRoleAttribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = true, 
    Inherited = true)]
public sealed class RequiresRoleAttribute : AuthorizationAttribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Method|AttributeTargets::Property|AttributeTargets::Field, AllowMultiple = true, 
    Inherited = true)]
public ref class RequiresRoleAttribute sealed : public AuthorizationAttribute
[<SealedAttribute>]
[<AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = true, 
    Inherited = true)>]
type RequiresRoleAttribute =  
    class
        inherit AuthorizationAttribute
    end
public final class RequiresRoleAttribute extends AuthorizationAttribute

The RequiresRoleAttribute type exposes the following members.

Constructors

Name Description
Public method RequiresRoleAttribute(String) Initializes a new instance of the RequiresRoleAttribute class that has the specified role.
Public method RequiresRoleAttribute(String()) Initializes a new instance of the RequiresRoleAttribute class that has the specified set of roles.

Top

Properties

Name Description
Public property ErrorMessage Gets or sets the literal error message or resource key intended to be returned in an ErrorMessage. (Inherited from AuthorizationAttribute.)
Public property ResourceType Gets or sets the Type to use as the resource manager for ErrorMessage. (Inherited from AuthorizationAttribute.)
Public property Roles Gets the roles that are permitted to invoke the operation.
Public property TypeId Gets a unique identifier for this attribute. (Overrides Attribute.TypeId.)

Top

Methods

Name Description
Public method Authorize Determines whether the given principal object is authorized to perform a specific operation described by the given AuthorizationContext. (Inherited from AuthorizationAttribute.)
Public method Equals (Inherited from Attribute.)
Protected method Finalize (Inherited from Object.)
Protected method FormatErrorMessage Gets the formatted error message for the current AuthorizationAttribute to present to the user. (Inherited from AuthorizationAttribute.)
Public method GetHashCode (Inherited from Attribute.)
Public method GetType (Inherited from Object.)
Protected method IsAuthorized Implementation specific method to determine whether the given IPrincipal object is authorized to perform a specific operation described by the given AuthorizationContext object. (Inherited from AuthorizationAttribute.)
Public method IsDefaultAttribute (Inherited from Attribute.)
Public method Match (Inherited from Attribute.)
Protected method MemberwiseClone (Inherited from Object.)
Public method ToString (Inherited from Object.)

Top

Explicit Interface Implementations

Name Description
Explicit interface implemetationPrivate method _Attribute.GetIDsOfNames (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfo (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfoCount (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.Invoke (Inherited from Attribute.)

Top

Remarks

You apply the RequiresRoleAttribute to a domain method to restrict access to the operation to only authenticated users that belong to one of the specified roles. When you apply the RequiresRoleAttribute to an entire domain service class, all of the domain operations are restricted to only authenticated users that belong to the specified roles. The RequiresRoleAttribute prevents the method from being executed when the user does meet the authentication criteria. If you call a domain operation when the user is not a member of the required role, the domain operation returns an exception. You can avoid this situation by checking the IsInRole method on the User object generated in the client project before calling the domain operation.

RIA Services also provides the RequiresAuthenticationAttribute to indicate that the user must be authenticated. You can provide customized authorization requirements by implementing a class that derives from AuthorizationAttribute and overriding the IsAuthorized method. For more information, see How to: Create a Custom Authorization Attribute.

Examples

The following example shows a domain service with the RequiresRoleAttribute attribute applied to the GetCustomers method.

<EnableClientAccess()>  _
Public Class AdventureWorksDomainService
    Inherits LinqToEntitiesDomainService(Of AdventureWorksLT_DataEntities)

    <RequiresRole("Managers")> _
    Public Function GetCustomers() As IQueryable(Of Customer)
        Return Me.ObjectContext.Customers
    End Function

    Public Function GetProducts() As IQueryable(Of Product)
        Return Me.ObjectContext.Products
    End Function

    <RequiresAuthentication()> _
    Public Function GetSalesOrderHeaders() As IQueryable(Of SalesOrderHeader)
        Return Me.ObjectContext.SalesOrderHeaders
    End Function
End Class
[EnableClientAccess()]
public class AdventureWorksDomainService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
{
    [RequiresRole("Managers")]
    public IQueryable<Customer> GetCustomers()
    {
        return this.ObjectContext.Customers;
    }

    public IQueryable<Product> GetProducts()
    {
        return this.ObjectContext.Products;
    }

    [RequiresAuthentication()]
    public IQueryable<SalesOrderHeader> GetSalesOrderHeaders()
    {
        return this.ObjectContext.SalesOrderHeaders;
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.ServiceModel.DomainServices.Server Namespace