Tasks 184281, 184799, 184800, 184801 and 184802
Align .editorconfig files Move Controller logic to DMO classes GlobalVars.AppSettings = Models.AppSettings.GetFromConfigurationManager(); Question EditorConfig Project level editorconfig Format White Spaces AppSetting when EnvironmentVariable not set Corrective Actions Tests Schedule Actions Tests DMO Tests Controller Tests Get ready to use VSCode IDE
This commit is contained in:
59
Fab2ApprovalSystem/DMO/AccountDMO.cs
Normal file
59
Fab2ApprovalSystem/DMO/AccountDMO.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Fab2ApprovalSystem.Models;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Fab2ApprovalSystem.DMO;
|
||||
|
||||
public class AccountDMO {
|
||||
|
||||
public static async Task<LoginResult> LoginAsync(HttpClient httpClient, LoginModel loginModel) {
|
||||
LoginResult result;
|
||||
|
||||
HttpRequestMessage request = new(HttpMethod.Post, "auth/login");
|
||||
|
||||
AuthAttempt authAttempt = loginModel is null ? null : new AuthAttempt() {
|
||||
LoginID = loginModel.LoginID,
|
||||
Password = loginModel.Password
|
||||
};
|
||||
|
||||
string json = authAttempt is null ? "{}" : JsonConvert.SerializeObject(authAttempt);
|
||||
|
||||
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(request);
|
||||
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
throw new Exception($"The authentication API failed, because {httpResponseMessage.ReasonPhrase}");
|
||||
|
||||
string responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
|
||||
result = JsonConvert.DeserializeObject<LoginResult>(responseContent);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async Task<LoginResult> ExternalAuthSetupAsync(HttpClient httpClient, AuthAttempt authAttempt) {
|
||||
LoginResult result;
|
||||
|
||||
HttpRequestMessage request = new(HttpMethod.Post, "auth/refresh");
|
||||
|
||||
string json = authAttempt is null ? "{}" : JsonConvert.SerializeObject(authAttempt);
|
||||
|
||||
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(request);
|
||||
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
throw new Exception($"The authentication API failed, because {httpResponseMessage.ReasonPhrase}");
|
||||
|
||||
string responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
|
||||
result = JsonConvert.DeserializeObject<LoginResult>(responseContent);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user