简体   繁体   English

登录后重定向:Web.config

[英]Redirect After Login : Web.config

In my ASP.NET Web Application, the project structure is shown by the following image: 在我的ASP.NET Web应用程序中,项目结构如下图所示:

在此输入图像描述

The Web.config of the Site has form authentication: 该站点的Web.config具有表单身份验证:

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

And the Web.config of the Pages folder has: Pages文件夹的Web.config具有:

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

I have an User admin with role Admin. 我有一个带有角色Admin的用户管理员。 After successful Login I am trying to redirect the user in Home.aspx resides in the Pages folder as: 成功登录后,我试图在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");
    }
}

But it is not working. 但它没有用。 It is again redirecting to the Login page ie, Login.aspx with the URL: localhost:3695/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx . 它再次重定向到Login页面,即Login.aspx,其URL为: localhost:3695/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx

How can I achieve this? 我怎样才能做到这一点? Any information will be very helpful. 任何信息都会非常有用。

Regards. 问候。

Membership.ValidateUser only validates the username and password against the membership provider. Membership.ValidateUser仅验证成员资格提供程序的用户名和密码。 It doesn't emit the authentication cookie. 它不会发出身份验证cookie。

If you want to do this you need to use the SetAuthCookie method before redirecting: 如果要执行此操作,则需要在重定向之前使用SetAuthCookie方法:

if (Membership.ValidateUser(UserNameTextBox.Text, PasswordTextBox.Text)) 
{
    FormsAuthentication.SetAuthCookie(UserNameTextBox.Text, false);
    Response.Redirect("~/Pages/Home.aspx");
}

or if in your web.config you set: 或者如果在你的web.config中你设置:

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

you could also use the RedirectFromLoginPage method which will emit the authentication cookie and redirect you to the default page: 您还可以使用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