Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
in my project i have login.aspx page and welcome.aspx page, when enter the credentials
and logged in, navigated to welcome.aspx page and in welcome.aspx page i have logout button, when i clicked on logout button, it navigated to login.aspx.but in browser i clicked button it is navigating to welcome.aspx page. so i want to clear the cookie or session, how to do this
send me the example..
Posted

Please refer following code,

After successful login add following code to add user data in session.
C#
HttpContext.Current.Session["UserID"] = data.UserId;
        HttpContext.Current.Session["Email"] = data.Email;


Add following code in master page.
C#
public bool checkAuthentication
    {
        get;
        set;
    } 

protected void Page_Load(object sender, EventArgs e)
    {        
        if (checkAuthentication)
        {
            if ((Session["UserID"] + "") == "")
            {
                Response.Write("<script type=\"text/javascript\">" +
                        "window.parent.location = '" + ConfigurationManager.AppSettings["SiteUrl"] + "login?loginUrl=' + window.parent.location + '&mode=session';" +
                                "</script>");
                Response.End();
            }
        }
}


Add following code in each child page where you want to authenticate user session.

C#
protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Master.checkAuthentication = true;
    }


On click of logout button redirect to logout page.
Add following code on page load of logout.aspx
C#
protected void Page_Load(object sender, EventArgs e)
   {
       Session.Abandon();
       Response.Redirect(ConfigurationManager.AppSettings["SiteUrl"] + "login?mode=logout",true);
   }


Hope this may help you.
 
Share this answer
 
Have a web.config to prevent the user to access the welcome page unless he is logged in.

alternatively in welcome.aspx.cs page_load check if no user is logged in redirect to login page.
 
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