Exemplo n.º 1
0
        public bool Authenticate(string email, string password, bool rememberMe = false)
        {
            bool result = repository.Authenticate(email, password);

            if (result)
            {
                User user = repository.GetUserByEmail(email);
                sRecipePrincipalSerializeModel userSearializeModel =
                    Mapper.Map <User, sRecipePrincipalSerializeModel>(user);

                string userData = JsonConvert.SerializeObject(userSearializeModel);
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                    1,
                    email,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(60),
                    rememberMe,              //pass here true, if you want to implement remember me functionality
                    userData);
                string     encTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                HttpContext.Current.Response.Cookies.Add(faCookie);
                // FormsAuthentication.SetAuthCookie(email, false);
            }
            return(result);
        }
Exemplo n.º 2
0
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                FormsAuthenticationTicket      authTicket         = FormsAuthentication.Decrypt(authCookie.Value);
                sRecipePrincipalSerializeModel userSerializeModel = JsonConvert.DeserializeObject <sRecipePrincipalSerializeModel>(authTicket.UserData);
                sRecipePrincipal userPrincipal = new sRecipePrincipal(authTicket.Name);
                userPrincipal.UserId     = userSerializeModel.UserId;
                userPrincipal.NickName   = userSerializeModel.NickName;
                userPrincipal.Role       = userSerializeModel.Role;
                userPrincipal.Profile    = userSerializeModel.Profile;
                HttpContext.Current.User = userPrincipal;
            }
        }