This commit is contained in:
2022-06-20 01:01:38 +00:00
parent ca0ce21d60
commit 4fb17a3778
2 changed files with 45 additions and 19 deletions

View File

@@ -55,7 +55,7 @@ namespace AyaNovaQBI
var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text); var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text);
if (!res) if (!res)
{ {
MessageBox.Show("AyaNova 8 SuperUser account login failed"); MessageBox.Show("Login failed");
btnLogin.Enabled = btnTest.Enabled = true; btnLogin.Enabled = btnTest.Enabled = true;
return; return;
} }

View File

@@ -79,26 +79,31 @@ namespace AyaNovaQBI
dynamic creds = new JObject(); dynamic creds = new JObject();
creds.login = login; creds.login = login;
creds.password = password; creds.password = password;
var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + "auth");
ApiResponse a = await PostAsync("auth", creds.ToString()); requestMessage.Content = new StringContent(creds.ToString(), System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
if (a.HttpResponse.IsSuccessStatusCode) try
{ {
JWT = a.ObjectResponse["data"]["token"].Value<string>(); response = await client.SendAsync(requestMessage);
//Must be *the* SuperUser to continue: }
a = await GetAsync("user/amsu"); catch (HttpRequestException ex)
var IsSuperUser = a.ObjectResponse["data"].Value<bool>(); {
if (!IsSuperUser) var Err = ex.Message;
{ var InnerErr = "";
JWT = string.Empty; if (ex.InnerException != null)
return false; InnerErr = ex.InnerException.Message;
} return false;
// throw new Exception("POST error, route: " + route + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace + "\r\nPOSTED OBJECT:\r\n" + postJson);
return true;
} }
//ApiResponse a = await PostAsync("auth", creds.ToString());
if (response.IsSuccessStatusCode)
{
var a= new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(await response.Content.ReadAsStringAsync()) };
JWT = a.ObjectResponse["data"]["token"].Value<string>();
return true;
}
return false; return false;
} }
@@ -207,7 +212,7 @@ namespace AyaNovaQBI
} }
private async static Task<ApiResponse> TryPostAsync(string route, string postJson = null) internal async static Task<ApiResponse> TryPostAsync(string route, string postJson = null)
{ {
var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route); var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route);
@@ -244,9 +249,30 @@ namespace AyaNovaQBI
/// <summary>
///
/// </summary>
/// <param name="jsonString"></param>
/// <returns></returns>
public static JObject Parse(string jsonString)
{
if (string.IsNullOrWhiteSpace(jsonString))
{
return null;
}
return JObject.Parse(jsonString);
}
public static long IdFromResponse(ApiResponse a)
{
return a.ObjectResponse["data"]["id"].Value<long>();
}
public static uint CTokenFromResponse(ApiResponse a)
{
return a.ObjectResponse["data"]["concurrency"].Value<uint>();
}