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:
<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:
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:
context.Session["USERNAME"] = null;
-> In each page of my website I use the condition bellow to check the user:
<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:
if( context.Session["USERNAME"] == null )
{
}
else
{
}
--> 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:
<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:
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 )
{
}
else
{
}
UpdateCookie:
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 !