WebSecurity - RequireRoles()


WebSecurity 对象WebSecurity Object

Definition

If the current user is not a member of all specified roles,RequireRoles()the method sets the HTTP status to 401 (Unauthorized).


C# and VB Syntax

WebSecurity.RequireRoles(roles)


Example

Example C#

WebSecurity.RequireRoles("Admin", "Poweruser");

Example VB

WebSecurity.RequireRoles("Admin", "Poweruser")


Parameters

Parameter Type Description
roles String A comma-separated list of roles that the current user must belong to.


Return Value

None.


Errors and Exceptions

In the following cases, any access toWebSecuritythe object will throw anInvalidOperationException:

  • InitializeDatabaseConnection()the method has not been called
  • SimpleMembershipNot initialized (or disabled in website configuration)

Remarks

RequireRoles()Verifies whether the current user is a member ofallof the specified roles. If the current user is notallmembers of the specified roles, the HTTP status is set to 401 (Unauthorized).

To verify whether the current user has been authenticated, use the RequireAuthenticatedUser()method.

To verify whether the current user is the specified user, use theRequireUser()method.


Technical Data

Name Value
Namespace WebMatrix.WebData
Assembly WebMatrix.WebData.dll


WebSecurity 对象WebSecurity Object Other Extensions