WebAuthenticator总是跳过登录页面,如何每次为新用户获取新的登录信息。
发布于 2021-09-06 06:13:38
您可以添加一个注销方法。我们检查MSAL为我们在本地缓存的所有可用帐户,并将它们注销。我们还清除了登录时存储在安全存储中的访问令牌。
public async Task<bool> SignOutAsync()
{
try
{
var accounts = await _pca.GetAccountsAsync();
// Go through all accounts and remove them.
while (accounts.Any())
{
await _pca.RemoveAsync(accounts.FirstOrDefault());
accounts = await _pca.GetAccountsAsync(); //The _pca is the PublicClientApplicationBuilder
}
// Clear our access token from secure storage.
SecureStorage.Remove("AccessToken");
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}更新:登录
public async Task<bool> SignInAsync()
{
try
{
var accounts = await _pca.GetAccountsAsync();
var firstAccount = accounts.FirstOrDefault();
var authResult = await _pca.AcquireTokenSilent(Scopes, firstAccount).ExecuteAsync();
// Store the access token securely for later use.
await SecureStorage.SetAsync("AccessToken", authResult?.AccessToken);
return true;
}
catch (MsalUiRequiredException)
{
try
{
// This means we need to login again through the MSAL window.
var authResult = await _pca.AcquireTokenInteractive(Scopes)
.WithParentActivityOrWindow(ParentWindow)
.ExecuteAsync();
// Store the access token securely for later use.
await SecureStorage.SetAsync("AccessToken", authResult?.AccessToken);
return true;
}
catch (Exception ex2)
{
Debug.WriteLine(ex2.ToString());
return false;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}https://stackoverflow.com/questions/69040406
复制相似问题