[英]Using forms authentication class for authentication
我正在開發一個用戶認證站點。 我有一個登錄頁面“ Login.aspx”,其中提供了一個登錄控件。 在web.config中,
<authentication mode="Forms">
<forms name=".AuthenticationCookie" loginUrl="Login.aspx" protection="All" timeout="60" path="/">
<credentials passwordFormat="Clear">
<user name="Jack" password="Jerry"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="*"/>
</authorization>
在login.aspx.cs頁面中,我提供了,
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (FormsAuthentication.Authenticate(Login1.UserName,Login1.Password))
{
FormsAuthentication.SetAuthCookie(Login1.UserName,true);
Label1.Text = "Login Successful";
Login1.InstructionText = "";
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
Response.Redirect("Success.aspx")
}
else
{
Label1.Text = "You are not an authentic user";
}
}
}
但是,但是,在執行而不是使用URL http://localhost/Login.aspx進入success.aspx ?ReturnUrl =%2fSuccess.aspx
為什么會這樣呢?
如果要自己設置表單身份驗證cookie並根據ReturnUrl查詢字符串參數正確重定向,則應查看FormsAuthentication.RedirectFromLoginPage方法。 在您的示例中,它將是:
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
該方法設置適當的Forms身份驗證cookie /票證,然后根據ReturnUrl參數的存在或不存在進行重定向。 (如果不存在,它將轉到已配置的默認頁面。)
希望這可以幫助,
唐尼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.