Wednesday, October 10, 2012

asp.net custom FormsAuthentication CustomPrincipal

in MembershipService class


                    if (b)
            {
                 Create the cookie that contains the forms authentication ticket
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, rememberMe);

                 Get the FormsAuthenticationTicket out of the encrypted cookie
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

                 Create a new FormsAuthenticationTicket that includes our custom User Data
                FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
                    ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, string.Empty);

                 Update the authCookie's Value to use the encrypted version of newTicket
                authCookie.Value = FormsAuthentication.Encrypt(newTicket);

                 Manually add the authCookie to the Cookies collection
                HttpContext.Current.Response.Cookies.Add(authCookie);
            }
or
            if (b)
            {
                CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                serializeModel.UserName = userName;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string userData = serializer.Serialize(serializeModel);

                DateTime expired = DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes);

                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                         1,
                         userName,
                         DateTime.Now,
                         expired,
                         rememberMe,
                         userData);

                string encTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                HttpContext.Current.Response.Cookies.Add(faCookie);
            }





in Global.asax


protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            //HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
           
            //if (authCookie != null)
            //{
            //    Response.Cookies.Add(authCookie);
            //    try
            //    {
            //        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

            //        JavaScriptSerializer serializer = new JavaScriptSerializer();

            //        CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);

            //        CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
            //        newUser.UserName = serializeModel.UserName;

            //        HttpContext.Current.User = newUser;
            //    }
            //    catch { HttpContext.Current.User = null; }
            //}
        }
       
        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            WebSites.Current.ConnectedUsers++;
            Application.UnLock();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            WebSites.Current.ConnectedUsers--;
            Application.UnLock();
        }


in login controller


                    //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    //1,
                    //model.UserName,
                    //DateTime.Now,
                    //DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                    //model.RememberMe,
                    //null);

                    //string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                    //this.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                    //this.Response.Cookies.Add(cookie);

No comments: