Ejemplo n.º 1
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var result = await WebApiProxy.ValidateUserPassword(model.Email, model.Password);

            switch (result.Status)
            {
            case HttpStatusCode.OK:
            {
                var user   = result.Data;
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.Hash, user.UserId.ToString()),
                    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                    new Claim(ClaimTypes.Thumbprint, user.AccessToken)
                };

                var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

                var ctx = Request.GetOwinContext();
                var authenticationManager = ctx.Authentication;
                authenticationManager.SignIn(id);
                FormsAuthentication.SetAuthCookie(user.UserName, false);

                return(RedirectToAction("ViewPerson", "Person", new { id = user.Id }));
            }

            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }