How to Redirect User to Login Page if User Directly Accesses Any Page in ASP.NET WebForms

asp.net c# entity-framework-6 webforms

Question

I am working on ASP.NET webforms. I want to redirect the user to login page if user directly enters the URL of any other page. I have added the below code in web.config file and it redirects the user to login page in case the user tries to access any other page directly but there is an issue that when user logs in, it should redirect the user to welcome.aspx page but it redirects to login page again. where is the issue?

code in web.config file i used....

<authentication mode="Forms">
  <forms name=".COOKIE" loginUrl="login.aspx" protection="All" path="/" timeout="480"/>
</authentication>
<authorization>
  <deny users="?"/>
  <allow users="*"/>
</authorization>

and login.aspx page code is as:

foreach (var user in db.usertables)

        if (user.username == TextBox1.Text && user.password == TextBox2.Text)
        {

            {
                Response.Redirect("welcome.aspx");
            }
        }
        else

        {

            Label1.Visible = true;

        }
1
0
10/23/2016 7:30:53 PM

Popular Answer

Remove the following code from web.config

<authorization>
 <deny users="?"/>
 <allow users="*"/>
</authorization>

In your aspx.cs page,add the following code

if(authenticated)
{    
    FormsAuthentication.SetAuthCookie(username,false);
    Response.Redirect("welcome.aspx");
}
0
4/20/2016 11:06:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow