Ejemplo n.º 1
0
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

                CustomPrincipalSerializeModel serializeModel = JsonConvert.DeserializeObject <CustomPrincipalSerializeModel>(authTicket.UserData);
                CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
                newUser.UserId    = serializeModel.UserId;
                newUser.FirstName = serializeModel.FirstName;
                newUser.LastName  = serializeModel.LastName;
                newUser.roles     = serializeModel.roles;

                HttpContext.Current.User = newUser;
            }
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            if(await securityManager.ValidateLoginAsync(email: model.Email, password: model.Password))
            {
                var identity = await securityManager.GetUserIdentityAsync(email: model.Email);
                var cookieIssueDate = DateTime.Now;
                if(identity != null && identity.IsAuthenticated)
                {
                    FormsAuthentication.SetAuthCookie(userName: model.Email, createPersistentCookie: false);
                    var principal = new CustomPrincipal(identity: identity, rolesArray: identity.Roles);
                    HttpContext.User = principal;
                    returnUrl = returnUrl ?? "/";
                    return Redirect(returnUrl);
                }
            }
            return new HttpUnauthorizedResult();
        }