角色身份验证在asp.net中不起作用
本文关键字:不起作用 net asp 身份验证 角色 | 更新日期: 2023-09-27 17:57:36
我使用下面的代码访问基于用户身份验证的页面库
if (user.FirstOrDefault() == HashedPassword)
{
string roles = "Member";
// Create the authentication ticket
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
loginName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(60),// Expiration
false, // Persistent
roles); // User data
// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// Create a cookie and add the encrypted ticket to the
// cookie as data.
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
// Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
Response.Redirect("/Members/ClientAccount.aspx");
}
else
{
Response.Redirect("signin.aspx");
}
}
如果登录详细信息正确,则会将用户定向到ClientAccount.aspx,但我希望只有当他/她的角色设置为Admin时,才会出现这种情况,如下面的web.config文件中所示。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="members.aspx">
<system.web>
<authorization>
<allow roles="Member" />
<allow roles="Admin" />
<deny users="?" />
</authorization>
</system.web>
</location>
<location path="ClientAccount.aspx">
<system.web>
<authorization>
<allow roles="Admin" />
<deny roles="Member"/>
<deny users="?" />
</authorization>
</system.web>
</location>
</configuration>
我该如何做到这一点?
我想web.config文件没有查看cookie来进行授权,所以我在那里做了一些错误的事情。
仔细检查您相对于web.config的位置路径,我猜这就是问题所在。
<location path="/Members/ClientAccount.aspx">
...
</location>
当然,你需要做其他事情,而不是这一行,我想你这样做只是为了测试?
Response.Redirect("/Members/ClientAccount.aspx");
即将它们重定向到您知道不允许它们访问的页面。我想,一旦你确定不允许会员访问该页面,你就会加强这一部分。
您应该确保您的web.config具有以下标签:
<authentication mode="Forms" />
你需要正确配置它,有很多选项:
<authentication mode="Forms">
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
http://msdn.microsoft.com/en-us/library/ff647070.aspx
嘿,你是说要吗
<拒绝角色="成员"/>
现在,拒绝策略确实不需要列出成员角色。如果你想让会员也被允许进入该页面,你需要换掉拒绝,以允许:
<authorization>
<allow roles="Admin" />
<allow roles="Member"/>
<deny users="?" />
</authorization>