2012-03-18 117 views
5

在我的ASP.NET Web应用程序,该项目结构由下图所示:重定向后登录:Web.config中

enter image description here

网站的Web.config有形式的认证:

<authentication mode="Forms"> 
    <forms loginUrl="~/Login.aspx" timeout="2880" />  
</authentication> 

并且页面文件夹的Web.config有:

<?xml version="1.0"?> 
<configuration> 
<system.web> 
    <authorization> 
    <allow roles="Admin"/> 
    <deny users="*"/> 
    </authorization> 
</system.web> 

我有一个用户管理员角色管理员。成功登录后,我试图重定向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.aspx,其URL为:localhost:3695/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx

我该如何做到这一点?任何信息都会非常有帮助。

问候。

回答

8

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); 
} 
+0

非常感谢... – 2012-03-18 14:53:47