/// <summary> /// Authenticate via straight HTTP Call /// </summary> /// <returns></returns> private async Task <AuthenticationHttpResponse> AuthenticateViaHttp(AuthenticationHttpRequest authRequest) { AuthenticationHttpResponse ret = null; StringContent content = new StringContent(authRequest.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded"); using (HttpResponseMessage httpResponse = await _HttpClient.PostAsync(CreateOAuthEndPoint(), content)) { string responseData = await httpResponse.Content.ReadAsStringAsync(); if (httpResponse.IsSuccessStatusCode) { ret = JsonSerializer.Deserialize <AuthenticationHttpResponse>(responseData); } else { // Some other non-successful status code. string msg = $"{(int)httpResponse.StatusCode} - {httpResponse.StatusCode.ToString()} ({responseData})"; throw new HttpRequestException(msg); } } return(ret); }
private async Task SetAuthToken(AuthenticationTypeEnum authenticationType) { // Guard Clause if (!String.IsNullOrWhiteSpace(_Token)) { return; } switch (authenticationType) { case AuthenticationTypeEnum.HTTP: AuthenticationHttpRequest httpRequest = new AuthenticationHttpRequest() { ClientId = _AzureADConfig.ClientId, ClientSecret = _AzureADConfig.ClientSecret, Scope = _CustomerApiConfig.Scope, }; AuthenticationHttpResponse httpResponse = await AuthenticateViaHttp(httpRequest); _Token = httpResponse.access_token; break; case AuthenticationTypeEnum.MSAL: AuthenticationResult msalResponse = await AuthenticateViaMsal(); _Token = msalResponse.AccessToken; break; default: throw new Exception("Token not set"); break; } }
public IActionResult Post([FromBody] AuthenticationHttpRequest request) { var user = _authService.GetToken(request.Username, request.Password); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } var response = new AuthenticationHttpResponse { Token = user.UserInformation.Token }; return(StatusCode((int)HttpStatusCode.OK, response)); }