public async Task <ActionResult> Login(LoginRequestModel requestModel) { try { IProvideUserRequestToLogin request = new ProvideUserRequestToLogin(requestModel.Login, requestModel.Password); IProvideUserResponse response = await _userProvider.GetUserByLoginAndPassword(request); if (response.Result != UserProvideResultEnum.Success) { return(Forbid()); } ClaimsIdentity claimsIdentity = PrepareClaimsWithPropertiesToSignIn(response, out var authProperties); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); return(Ok()); } catch (Exception ex) { _logger.Log(ex); return(StatusCode(500)); } }
private ActionResult PrepareResponseAfterGetUser(IProvideUserResponse provideUserResponse) { return(provideUserResponse.Result switch { UserProvideResultEnum.Success => (ActionResult)Ok( _objectToApiModelConverter.ConvertUser(provideUserResponse.User)), UserProvideResultEnum.NotFound => StatusCode(404), UserProvideResultEnum.Exception => StatusCode(500), UserProvideResultEnum.Forbidden => StatusCode(403), _ => throw new ArgumentOutOfRangeException() });
public async Task <ActionResult> GetUser() { try { Guid loggedInUserId = _userIdFromClaimsExpander.ExpandIdFromClaims(HttpContext.User); IProvideUserResponse provideUserResponse = await _userProvider.GetUserById( new ProvideUserRequest(loggedInUserId)); return(PrepareResponseAfterGetUser(provideUserResponse)); } catch (Exception ex) { _logger.Log(ex); return(StatusCode(500)); } }
private static ClaimsIdentity PrepareClaimsWithPropertiesToSignIn(IProvideUserResponse response, out AuthenticationProperties authProperties) { IList <Claim> claims = new List <Claim> { new Claim("Id", response.User.Id.ToString()), new Claim("Email", response.User.Email) }; ClaimsIdentity claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); authProperties = new AuthenticationProperties { //AllowRefresh = <bool>, // Refreshing the authentication session should be allowed. //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), // The time at which the authentication ticket expires. A // value set here overrides the ExpireTimeSpan option of // CookieAuthenticationOptions set with AddCookie. //IsPersistent = true, // Whether the authentication session is persisted across // multiple requests. When used with cookies, controls // whether the cookie's lifetime is absolute (matching the // lifetime of the authentication ticket) or session-based. //IssuedUtc = <DateTimeOffset>, // The time at which the authentication ticket was issued. //RedirectUri = <string> // The full path or absolute URI to be used as an http // redirect response value. }; return(claimsIdentity); }