【发布时间】:2016-02-18 03:51:30
【问题描述】:
所以我正在为我的asp.net 网站制作一个登录系统。有 3 种不同类型的用户。我发现 FORMS 可以管理角色,所以我决定尝试一下。
我目前在 FORMS 中使用身份验证的所有内容 - 但没有角色。我发现这段代码应该限制对特定页面的访问。但是每个人仍然可以访问该页面。这很奇怪,因为我没有将任何人添加到角色“成员”中。一开始我只添加了 1 个角色来查看是否有人被阻止访问该页面。
<configuration>
<connectionStrings>
//EDITED
</connectionStrings>
<system.web>
<roleManager enabled="true" />
<customErrors mode ="Off">
</customErrors>
<authentication mode="Forms">
<forms name=".ASPXAUTH"
loginUrl="login.aspx"
protection="All"
timeout="30"
path="/">
</forms>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
</system.web>
<location path="RandomPage.aspx">
<system.web>
<authorization>
<allow roles="Member" />
<deny users="*" />
</authorization>
</system.web>
</location>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
将角色添加到 FormsAuthenticationTicket 的代码。 P.Userole 包含字符串“Member”
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //Ticket version
p.firstName, //username
DateTime.Now,
DateTime.Now.AddMinutes(30),
false, //true for persistant user cookie
p.userRole+"",
FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
【问题讨论】: