public async Task LogIn() { ErrorMessages = null; var loginModel = new LoginModel() { EmailAddress = UserName, Password = Password }; try { var token = await _authenticationEndpoint.GetToken(loginModel); _usuarioLogged.Set(userName: token.UserName, token: token.FullToken); _events.PublishOnUIThread(new LogOnEvent()); } catch (UnauthorizedRequestException) { ErrorMessages = new BindingList <string> { "No tiene acceso" }; } catch (BadRequestException ex) { ErrorMessages = new BindingList <string>(ex.Errors.Select(kvp => string.Join(". ", kvp.Value)).ToList()); } catch (Exception ex) { ErrorMessages = new BindingList <string> { $"{ex.Message} Ha ocurrido un error. Por favor contacte a soporte" }; } }
public async Task <ActionResult> Login(LoginModel model, string returnUrl) { try { var token = await _authenticationEndpoint.GetToken(model); var roles = await _authenticationEndpoint.GetUserRoles(token.FullToken); AuthenticationProperties options = new AuthenticationProperties(); options.AllowRefresh = true; options.IsPersistent = model.RememberMe; if (options.IsPersistent) { options.ExpiresUtc = DateTime.UtcNow.AddSeconds(int.Parse(token.Expires_in)); } var claims = new List <Claim>() { new Claim(type: ClaimTypes.Name, value: model.EmailAddress), new Claim(type: "AcessToken", value: token.FullToken), }; foreach (string role in roles) { claims.Add(new Claim(type: ClaimTypes.Role, value: role)); } var identity = new ClaimsIdentity(claims: claims, authenticationType: "ApplicationCookie"); Request.GetOwinContext().Authentication.SignIn(properties: options, identities: identity); return(RedirectToAction("Dashboard", "Home")); } catch (BadRequestException ex) { ModelState.AddModelErrors(ex.Errors); return(View(model)); } }