工作我使用下面的代碼來訪問基於用戶身份驗證角色的身份驗證是不是在asp.net
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來執行授權,所以我在那裏做錯了什麼。
是它我還是你沒有檢查,看看用戶是否有管理角色?做一個檢查,然後根據角色重定向。 – Gage 2011-07-28 13:33:33