public async Task <IActionResult> Login(string returnUrl) { var vm = await LoginVMFactory.BuildLoginVMAsync(_securableService, _schemeProvider, returnUrl); if (vm.IsExternalLoginOnly) { // we only have one option for logging in and it's an external provider return(await ExternalLogin(vm.ExternalLoginScheme, returnUrl)); } return(View(vm)); }
public async Task <IActionResult> Login(LoginInputVM model, string button) { if (button != "login") { var returnUrl = await _accountService.CancelLoginAsync(model.ReturnUrl); return(Redirect(returnUrl)); } if (ModelState.IsValid) { var result = await _accountService.LoginAsync(AutoMapper.Mapper.Map <LoginSM>(model)); if (result.Succeeded) { // only set explicit expiration here if user chooses "remember me". // otherwise we rely upon expiration configured in cookie middleware. AuthenticationProperties props = null; if (AccountOptionsOM.AllowRememberLogin && model.RememberLogin) { props = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptionsOM.RememberMeLoginDuration) }; } ; // issue authentication cookie with subject ID and username -- and roles var roles = await _accountService.GetRolesForUserAsync(result.User); var claimsId = new ClaimsIdentity(); claimsId.AddClaim(new Claim(JwtClaimTypes.Subject, result.User.Id)); claimsId.AddClaim(new Claim(JwtClaimTypes.Name, result.User.UserName)); var roleClaims = roles.Select(x => new Claim(JwtClaimTypes.Role, x)); claimsId.AddClaims(roleClaims); await HttpContext.SignInAsync( //result.User.Id, new ClaimsPrincipal(claimsId), props); //result.User.UserName, props); // make sure the returnUrl is still valid, and if so redirect back to authorize endpoint or a local page // the IsLocalUrl check is only necessary if you want to support additional local pages, otherwise IsValidReturnUrl is more strict if (_accountService.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl)) { return(Redirect(model.ReturnUrl)); } return(Redirect("~/")); } ModelState.AddModelError("", AccountOptionsOM.InvalidCredentialsErrorMessage); } // something went wrong, show form with error var vm = await LoginVMFactory.BuildLoginVMAsync(_securableService, _schemeProvider, model); return(View(vm)); }