[英]Redirect After Login : Web.config
在我的ASP.NET Web應用程序中,項目結構如下圖所示:
該站點的Web.config具有表單身份驗證:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="2880" />
</authentication>
Pages文件夾的Web.config具有:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="Admin"/>
<deny users="*"/>
</authorization>
</system.web>
我有一個帶有角色Admin的用戶管理員。 成功登錄后,我試圖在Home.aspx中重定向用戶駐留在Pages文件夾中:
protected void EMSLogin_Authenticate(object sender, AuthenticateEventArgs e) {
TextBox UserNameTextBox = EMSLogin.FindControl("UserName") as TextBox;
TextBox PasswordTextBox = EMSLogin.FindControl("Password") as TextBox;
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text)) {
Response.Redirect("~/Pages/Home.aspx");
}
}
但它沒有用。 它再次重定向到Login頁面,即Login.aspx,其URL為: localhost:3695/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx
。
我怎樣才能做到這一點? 任何信息都會非常有用。
問候。
Membership.ValidateUser
僅驗證成員資格提供程序的用戶名和密碼。 它不會發出身份驗證cookie。
如果要執行此操作,則需要在重定向之前使用SetAuthCookie
方法:
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.SetAuthCookie(UserNameTextBox.Text, false);
Response.Redirect("~/Pages/Home.aspx");
}
或者如果在你的web.config中你設置:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="~/Pages/Home.aspx" timeout="2880" />
</authentication>
您還可以使用RedirectFromLoginPage
方法,該方法將發出身份驗證cookie並將您重定向到默認頁面:
if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text))
{
FormsAuthentication.RedirectFromLoginPage(UserNameTextBox.Text, false);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.