public async Task <ActionResult <CompanyAuthenticationProfileModel> > SignIn([FromBody] CompanySignInModel companySignInModel) => await Exec(async operation => { if (User.Identity.IsAuthenticated) { throw new Exception(ExceptionMessage.UserHasAlreadyAuthenticated); } try { if (!ModelState.IsValid) { throw new Exception(ExceptionMessage.CompanySignInFailedDueToInvalidModel); } var profile = await userAuthenticationService.TrySignIn(operation, companySignInModel.ToEntity()); await LoginChallenge(profile, UserRole.Company); return(new CompanyAuthenticationProfileModel { YandexMapsApiKey = configuration["Plugins:YandexMaps:ApiKey"], DadataApiKey = configuration["Plugins:DaData:ApiKey"] }.ToModel(profile)); } catch { await LogoutChallenge(); throw; } });
public async Task <IActionResult> SignIn([FromBody] CompanySignInModel company) { using (var client = clientHelper.GetServiceSecuredClient()) { var authResp = await client.PostAsync(this.routeTable.GetRoute(SvcRouteTable.CompanySignIn), new StringContent(JsonConvert.SerializeObject(new { CompanyIdentifier = company.CompanyIdentifier, Password = company.Password, }), Encoding.UTF8, "application/json")); if (!authResp.IsSuccessStatusCode) { return(new StatusCodeResult((int)authResp.StatusCode)); } var u = JsonConvert.DeserializeObject <ApiResponse <CompanyResponseModel> >(await authResp.Content.ReadAsStringAsync()); if (!u.Success || u.Result == null) { return(new UnauthorizedResult()); } var claims = new List <Claim> { new Claim("company", u.Result.CompanyIdentifier), }; foreach (var userClaim in User.Claims) { claims.Add(userClaim); } var userIdentity = new ClaimsIdentity(claims); ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync(principal); } return(Ok()); }