WebSecurity RequireRoles method
definition
If the current user is not a member of any of the specified role, RequireRoles () method sets the HTTP status of 401 (Unauthorized).
C # and VB syntax
WebSecurity.RequireRoles(roles)
Examples
Examples of C #
WebSecurity.RequireRoles("Admin", "Poweruser");
Examples VB
WebSecurity.RequireRoles("Admin", "Poweruser")
parameter
参数 | 类型 | 描述 |
---|---|---|
roles | String | 当前用户必须属于的角色列表,以逗号分隔。 |
return value
no.
Errors and exceptions
In the following scenario, any access to WebSecurity object will throw an InvalidOperationException:
- InitializeDatabaseConnection () method has not been called
- SimpleMembership not initialized (or disabled in the site configuration)
Remark
RequireRoles () verifies that the current user is a member of all of the specified role. If the current user is not all members of the specified role, the HTTP status is set to 401 (Unauthorized).
To verify whether the current user has been authenticated, use RequireAuthenticatedUser () method.
To verify that the current user is the user specified, use RequireUser () method.
Technical data
名称 | 值 |
---|---|
Namespace | WebMatrix.WebData |
Assembly | WebMatrix.WebData.dll |
WebSecurity objects