Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am storing a user name into cache["sKey"] in login page if this cache variable is empty then will go to login page.But after log out have to clear this object data.I tried with below

C#
public void empLogin()
        {
            try
            {
string sKey = txtUName.Text + txtPwd.Text;
                            string sUser = Convert.ToString(Cache[sKey]);

                            if (sUser == null || sUser == String.Empty)
                            {
                                TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
                                HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut,
                                System.Web.Caching.CacheItemPriority.NotRemovable, null);
Session["user"] = txtUName.Text + txtPwd.Text;
if (userType == "Admin")
{
  Response.Redirect("~/Admin/DashBoard.aspx");
}
 }
else
   {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('This User Alredy logged in');", true);
   }


in logut button click.

C#
protected void linkLogout_Click(object sender, EventArgs e)
{
  Cache["sKey"] = string.Empty;
  Response.Redirect("~/LoginPage.aspx");
}
Posted

change Cache.Insert as below
C#
HttpContext.Current.Cache.Insert("sKey", sKey, null, DateTime.MaxValue, SessTimeOut,
                                System.Web.Caching.CacheItemPriority.NotRemovable, null);

current problem is your Cache key name is not "sKey" it is combination of txtUName.Text and txtPwd.Text
 
Share this answer
 
v2
Comments
abbas azaad 18-May-15 4:54am    
if I clear all cache value then text boxes wouldnt save name pwd which is working on check change event of check box.
abbas azaad 18-May-15 4:54am    
mean in "remember me" option.
DamithSL 18-May-15 5:14am    
check my updated answer
abbas azaad 18-May-15 5:39am    
so in this i can remove username and pwd from session and can save textboxes values if checked check box...so the above code where have to implement?
abbas azaad 18-May-15 5:48am    
very very thanq ...i got it now..
Your code is a bit all over the place. You are storing the username in the Session and also the Cache? Secondly your cache key is username + password so if I log in the cache key is "usernamepassword", but in your logout click you are clearing the key called "skey". This is the literal string "sKey", not the contents of the variable sKey which does not exist in your function. To clear from the cache you use Cache.Remove but you need to know the key to remove, and you don't as your key is dynamic. Also cache is the same for everyone, it isn't per-user.

My advice is to ignore the cache, just store things in the session and on your logout link

Session.Abandon()
 
Share this answer
 
Comments
abbas azaad 18-May-15 4:56am    
session.abandon also not working.
F-ES Sitecore 18-May-15 5:03am    
You'll only see the session abandoned at the next page request, it doesn't abandon immediately.
Cache.Remove("Key")
 
Share this answer
 
Comments
abbas azaad 18-May-15 5:36am    
its not working

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