繁体   English   中英

登录后重定向:Web.config

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM