Login redirection: Web.config

In my ASP.NET web application, the project structure is displayed as follows:

enter image description here

Web.config Site has form authentication:

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

And the Web.config folder in the Pages folder:

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

I have a user admin with the admin role. After a successful login, I try to redirect the user to Home.aspx in the Pages folder:

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 does not work. Again, he is redirected to the login page, that is Login.aspx are URL-address: localhost:3695/Login.aspx?ReturnUrl=%2fPages%2fHome.aspx.

How can i achieve this? Any information would be very helpful.

Sincerely.

+5
source share
1 answer

Membership.ValidateUserverifies only the username and password for the membership provider. It does not generate authentication cookies.

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

All Articles