WebSecurity - RequireRoles()
WebSecurity object
define
If the current user is not a member of all the 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
| Parameters | Type | Description |
|---|---|---|
| roles | String | A comma-separated list of roles the current user must belong to. |
Return Value
none.
Errors and exceptions
In the following cases, any access to the WebSecurity object will throw an InvalidOperationException:
- InitializeDatabaseConnection() The method is not called
- SimpleMembership Not initialized (or disabled in the website configuration)
Notes
RequireRoles() Verifies whether the current user is a member ofallthe specified roles. If the current user is not a member ofallthe specified roles, the HTTP status is set to 401 (Unauthorized).
To verify whether the current user is authenticated, use the **RequireAuthenticatedUser()** method.
To verify whether the current user is the specified user, use the RequireUser() method.
Technical data
| Name | Value |
|---|---|
| Namespace | WebMatrix.WebData |
| Assembly | WebMatrix.WebData.dll |
WebSecurity object