Hi,
I need to obtain the user's account and password when logging in to verify another system and perform API docking, but I need to save the obtained token in the session so that it can be saved when accessing the onloggedon method for the second time. But I found that no matter what method is used, it will cause the page to get stuck in the loading login interface.
Unless asynchronous methods are used instead of synchronous methods.
How can I wait for the asynchronous method to complete before executing base.onloggedon?
C#protected override void OnLoggedOn(LogonEventArgs args)
{
AuthenticationStandardLogonParameters logonParameters = (AuthenticationStandardLogonParameters)args.LogonParameters;
var sessionStorageService = ServiceProvider.GetRequiredService<ProtectedSessionStorage>();
var commonWeb = ServiceProvider.GetRequiredService<ICommonWeb>();
var principalProvider = ServiceProvider.GetRequiredService<IPrincipalProvider>();
//AzureAD
if (AzureADAuthenticationProvider.CanHandlePrincipal(principalProvider.User))
{
string sid = commonWeb.NasLogon_Azure(this);
sessionStorageService.SetAsync("sid", sid);
}
//Password
else
{
if (string.IsNullOrEmpty(logonParameters.UserName) == false && string.IsNullOrEmpty(logonParameters.Password) == false)
{
//string sid = commonWeb.NasLogon_Pwd(this, logonParameters.UserName, logonParameters.Password);
//sessionStorageService.SetAsync("sid", sid);
IsLoging = true;
var httpclient = commonWeb.WebApiLogon(logonParameters.UserName, logonParameters.Password);
sessionStorageService.SetAsync("webapi_token", httpclient);
}
}
try
{
//using this code will stuck in the loading scress
var protectedBrowserStorageResult = sessionStorageService.GetAsync<string>("sid").ConfigureAwait(false).GetAwaiter().GetResult();
if (protectedBrowserStorageResult.Success)
{
var httpClientFactory = this.ServiceProvider.GetRequiredService<IHttpClientFactory>();
string sid = protectedBrowserStorageResult.Value;
FileStationHelper fileStationHelper = new FileStationHelper(httpClientFactory, sid, commonWeb.NAS_Admin_SID);
commonWeb.NAS_SID = sid;
commonWeb.FileServer = fileStationHelper;
}
var session_httpclient = sessionStorageService.GetAsync<string>("webapi_token").ConfigureAwait(false).GetAwaiter().GetResult();
if (session_httpclient.Success)
{
commonWeb.WebApiToken = session_httpclient.Value;
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
IsLoging = false;
}
//using this code will not stuck
//Task.Run(async () =>
//{
// try
// {
// var protectedBrowserStorageResult = await sessionStorageService.GetAsync<string>("sid");
// if (protectedBrowserStorageResult.Success)
// {
// var httpClientFactory = this.ServiceProvider.GetRequiredService<IHttpClientFactory>();
// string sid = protectedBrowserStorageResult.Value;
// FileStationHelper fileStationHelper = new FileStationHelper(httpClientFactory, sid, commonWeb.NAS_Admin_SID);
// commonWeb.NAS_SID = sid;
// commonWeb.FileServer = fileStationHelper;
// }
// var session_httpclient = await sessionStorageService.GetAsync<string>("webapi_token");
// if (session_httpclient.Success)
// {
// commonWeb.WebApiToken = session_httpclient.Value;
// }
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// throw;
// }
// finally
// {
// IsLoging = false;
// }
//});
base.OnLoggedOn(args);
}