首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebAuthenticator总是跳过登录页面,如何为新用户获取新的登录表单?

WebAuthenticator总是跳过登录页面,如何为新用户获取新的登录表单?
EN

Stack Overflow用户
提问于 2021-09-03 06:27:06
回答 1查看 43关注 0票数 0

WebAuthenticator总是跳过登录页面,如何每次为新用户获取新的登录信息。

EN

回答 1

Stack Overflow用户

发布于 2021-09-06 06:13:38

您可以添加一个注销方法。我们检查MSAL为我们在本地缓存的所有可用帐户,并将它们注销。我们还清除了登录时存储在安全存储中的访问令牌。

代码语言:javascript
复制
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;
}
}

更新:登录

代码语言:javascript
复制
      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;
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69040406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档