[英]Roles authentication is not working in 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,但我希望只有當他/她的角色設置為管理員時才會發生這種情況,如下面的 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>
嘿那里,你的意思是
<拒絕角色="會員"/>
現在,拒絕策略確實不需要列出成員角色。 如果您希望成員也被允許訪問該頁面,則需要換出拒絕,以允許:
<authorization>
<allow roles="Admin" />
<allow roles="Member"/>
<deny users="?" />
</authorization>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.