public async Task <AuthenticationResponse> Login(Principal principal) { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PostAsJsonAsync(_userServiceUrl, principal).Result; CheckPrincipalResponse res = await response.Content.ReadFromJsonAsync <CheckPrincipalResponse>(); if (!response.IsSuccessStatusCode) { return(new AuthenticationResponse { Success = false, Error = res.Message.ToString() }); } Guid id = res.AccountInfo.Id; string role = res.AccountInfo.Role; DateTime dateIssued = DateTime.UtcNow; AuthInfo user = _authRepository.GetAuthInfoByUserId(id); if (user != null) { return(new AuthenticationResponse { Token = user.Token, Success = true }); } var token = IssueToken(id.ToString(), role); AuthInfo authInfo = new AuthInfo { Id = id, Role = role, Token = token, TimeOfIssuingToken = dateIssued }; _authRepository.CreateAuthInfo(authInfo); return(new AuthenticationResponse { Token = token, Success = true }); } }
public async Task <AuthenticationResponse> Login(Principal principal) { using (HttpClient client = new HttpClient()) { Uri url = new Uri($"{ _configuration["Services:UserService"] }api/accounts/checkPrincipal"); HttpContent content = new StringContent(JsonConvert.SerializeObject(principal)); content.Headers.ContentType.MediaType = "application/json"; HttpResponseMessage response = client.PostAsync(url, content).Result; CheckPrincipalResponse res = await response.Content.ReadFromJsonAsync <CheckPrincipalResponse>(); if (!response.IsSuccessStatusCode) { return(new AuthenticationResponse { Succes = false, Error = res.Message.ToString() }); } string publicToken = Guid.NewGuid().ToString(); Guid id = res.AccountInfo.Id; string role = res.AccountInfo.Role; DateTime dateIssued = DateTime.UtcNow; AuthInfo user = _authInfoRepository.GetAuthInfoByUserId(id); if (user != null) { return(new AuthenticationResponse { Token = user.PublicToken, Succes = true }); } AuthInfo authInfo = new AuthInfo { UserId = id, Role = role, PublicToken = publicToken, TimeOfIssuingPublicToken = dateIssued }; _authInfoRepository.CreateAuthInfo(authInfo); return(new AuthenticationResponse { Token = publicToken, Succes = true }); } }