Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi friends.

I use session to remember the user who is signed in to my website, when he is going through pages in my website.

I use AJAX to sign in:

JavaScript
<script type="text/javascript">
 var url = "SignIn.ashx?username=" + UserName.value + "&password=" + PassWord.value;
     xmlRequest_SignIn.open("GET", url);
     xmlRequest_SignIn.onreadystatechange = function () { ApplyUpdateSignIn() }
     xmlRequest_SignIn.send(null);
 </script>

and in SignIn.ashx file I use this code for log in part:

C#
// log in
    context.Session["USERNAME"] = UserName.toString();


note that I've embedded System.Web.SessionState.IRequiresSessionState

and for signout I use the same script,
and in SignOut.ashx file I use this code for log out:

C#
context.Session["USERNAME"] = null;


-> In each page of my website I use the condition bellow to check the user:

JavaScript
<script type="text/javascript">
    var url = "checkMember.ashx?";
        xmlRequest_checkMember.open("GET", url);
        xmlRequest_checkMember.onreadystatechange = function () { ApplyUpdatecheckMember() }
        xmlRequest_checkMember.send(null);
   </script>


checkMember.ashx:

C#
if( context.Session["USERNAME"] == null )
{
    //user didn't logged in yet
}
else
{
    //user has been logged in
}


--> this code is work well in all browsers EXCEPT IE !!! (browsers like Chrome, Mozilla, Safari, Opera, ... )

in IE when user go to another page, or refresh the current page, the condition in checkMember.ashx returns true value, and it cant remember user Signed it in previous page !!

Note that cookie is enabled in IE browser.

After searching alot, I found this solution:

JavaScript
<script type="text/javascript">
       var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
       var ASPSESSID = "<%= Session.SessionID %>";

       var url = "checkMember.ashx?ASPSESSID=" + ASPSESSID.toString() + "&AUTHID=" + auth.toString();
       xmlRequest_checkMember.open("GET", url);
       xmlRequest_checkMember.onreadystatechange = function () { ApplyUpdatecheckMember() }
       xmlRequest_checkMember.send(null);
  </script>


checkMember.ashx:

C#
string session_cookie_name = "ASP.NET_SessionId";
           if (context.Request.QueryString["ASPSESSID"].ToString() != null)
           {
               UpdateCookie(session_cookie_name, context.Request.QueryString["ASPSESSID"].ToString());
           }

           string auth_cookie_name = FormsAuthentication.FormsCookieName;
           if (context.Request.QueryString["AUTHID"].ToString() != null)
           {
               UpdateCookie(auth_cookie_name, context.Request.QueryString["AUTHID"].ToString());
           }

    if( context.Session["USERNAME"] == null )
    {
        //user didn't logged in yet
    }
    else
    {
        //user has been logged in
    }


UpdateCookie:

C#
private void UpdateCookie(string cookie_name, string cookie_value)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
        if (null == cookie)
        {
            cookie = new HttpCookie(cookie_name);
        }
        cookie.Value = cookie_value;
        HttpContext.Current.Request.Cookies.Set(cookie);
    }


Now this code is works well for IE too,

BUT ! I cant signOut in IE !!

the problem I mentioned is ON now for signOut !
when the user sign out and then goes to another page, the code above cant understand that the session is null and the user has logged out !

what should I do?

please help me !
Posted
Updated 20-May-13 1:19am
v2
Comments
Thanks7872 20-May-13 11:38am    
Well framed question.Found rare this days.Appreciated.
Mohamad77 20-May-13 23:39pm    
Thanks, But no answer yet !

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