复制现有应用程序的 Global.asax 文件中的以下代码,将这些角色分配给 Application_AuthenticateRequest 事件处理程序中的验证用户: Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
if (not(HttpContext.Current.User is Nothing)) then
if HttpContext.Current.User.Identity.AuthenticationType = "Forms" then
Dim id as System.Web.Security.FormsIdentity
id = HttpContext.Current.User.Identity
Dim MyRoles(2) As String
MyRoles(0) = "Manager"
MyRoles(1) = "Admin"
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id,MyRoles)
End if
End if
End sub
返回页首
检查用户角色并在 ASPX 页中实现程序逻辑
下列步骤说明如何根据验证用户所属的角色实现并控制程序逻辑。1. 创建名称为 Sample.aspx 的新 .aspx 页,并粘贴以下代码: <%@ Page Language="VB" %>
<%@ Import Namespace="System.Web" %>
<script runat=server>
Sub Page_Load(Src As Object, E As EventArgs)
if User.IsInRole("Admin") then
Response.Write ("You are an Administrator")
Else
Response.Write ("You do not have any role assigned")
End if
End Sub
</script>
2. 将 Sample.aspx 保存到现有应用程序中。浏览到该页以测试它。