Thursday, 22 December 2011

Remember me password :::::::::::::::::::>>>>

using System;
using System.Web;
using System.Web.Security;


1.if (HttpContext.Request.Cookies[model.UserName] != null)
                    {
                        HttpCookie ckLastLoginFailDate = new HttpCookie(model.UserName.Trim(), DateTime.Now.ToString());
                        ckLastLoginFailDate.Expires = DateTime.Now.AddDays(-2);
                    }

2.   public void SignIn(string userName, string userData, string returnUrl, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), createPersistentCookie, userData);
            string encTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            HttpContext.Current.Response.Cookies.Add(faCookie);

            if (createPersistentCookie) //if RememberMe checked
            {
                //Set Username in Cookie
                HttpCookie cookie = new HttpCookie("UserCookie") { Value = userName, Expires = DateTime.Now.AddDays(7) };
                HttpContext.Current.Response.Cookies.Add(cookie);
                //set Return Url in Cookie
                HttpCookie urlcookie = new HttpCookie("UrlCookie")
                {
                    Value = returnUrl,
                    Expires = DateTime.Now.AddDays(7)
                };
                HttpContext.Current.Response.Cookies.Add(urlcookie);
            }
            else
            {
                //Remove User Cookie
                HttpCookie cookie = new HttpCookie("UserCookie") { Expires = DateTime.Now.AddDays(-1) };
                HttpContext.Current.Response.Cookies.Set(cookie);
                //Remove ReturnUrl Cookie
                HttpCookie urlcookie = new HttpCookie("UrlCookie") { Expires = DateTime.Now.AddDays(-1) };
                HttpContext.Current.Response.Cookies.Set(urlcookie);
            }

            //Set Username in Session
            UserEntity objUser = new UserEntity { UserName = userName };

            HttpContext.Current.Session["User"] = objUser;

        }

3.   if (HttpContext.Request.Cookies.AllKeys.Contains("UserCookie"))
            {
                model.UserName = HttpContext.Request.Cookies["UserCookie"].Value;
                model.RememberMe = true;
            }
 

No comments:

Post a Comment