Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all ,

I want to know about login url code.

In my project i ve a login page ,if i signin in mozilla and copy the url and paste it into other website lik chrome and IE mean ,it displayed the singin page i.e my inbox content page ,but it should not display the same and should display my Home page (login page), i need to redirect the login page,how can i do this .


Help me...
Posted
Updated 21-Jan-12 2:22am
v2

Read this article:

ASP.NET Membership and Role Provider[^]

or

use this code in web.config file
XML
<authentication mode="Forms">
     <forms cookieless="UseCookies" defaultUrl="HomePage.aspx"
    loginUrl="UnAuthorized.aspx" protection="All" timeout="30">
          </forms>
</authentication>


and use login control event with following code:
C#
protected void Login1_Authenticate(object sender,AuthenticateEventArgs e)
{
 if (Membership.ValidateUser(Login1.UserName, Login1.Password) == true)
    {
        Login1.Visible = true;
        Session["user"] = User.Identity.Name;
        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
    }
 else
    {
        Response.Write("Invalid Login");
    }
}
 
Share this answer
 
Thank You all, I am using like the below, its working great.
C#
        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
 
Share this answer
 
v2
Comments
André Kraak 21-Jan-12 9:14am    
Edited answer:
Added pre tags
You need to create a session variable when the user logs in. This variable is probably Session["UserName"]. On the Page_Load of every page, you should check the variable for null and else redirect the user to the login page

C#
if(Session["UserName"]==null)
{
Response.Redirect("Login.aspx");
}


Since it is not convenient to write this code on each and every page, write it in the base page.

See the base page concept here

C#
public class SessionCheck : System.Web.UI.Page
{
    override protected void OnInit(EventArgs e)
    {
        //initialize our base class (System.Web,UI.Page)
        base.OnInit(e);
        //check to see if the Session is null (doesnt exist)
        if (Context.Session != null)
        {
if(Session["UserName"]==null)
{
Response.Redirect("Login.aspx");
}
            }
    }
}
 
Share this answer
 
 
Share this answer
 
On your inbox page put this condition on Page_Init
if(Session["UserName"]==null)
{
Session.Clear();
Session.Abondon();
Response.Redirect("~/Login.apsx");
}
 
Share this answer
 
when we copy the url to another browser it should be redirected to login page only.

in my project ,Its working but have some problem.if i run my project in chrome and copy the url in mozilla ,it redirected into my login page.but in mozilla if i open a new tab and copy the url it shows my current page instead of login page.once i close all tab in mozilla and do the same thing mean it is working .Why this problem happening pls help me.


Now am using lik this :


C#
protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}



.Shall i use any other method in web config ,pls suggest me  .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900