Azure AD 身份验证删除保存的用户凭据

问题描述 投票:0回答:1

在我的UWP应用程序中,我为用户添加了Azure AD身份验证。用户可以正确登录。但是,一旦用户下次勾选“保持登录”选项,只需单击登录按钮,用户就可以登录。在哪里这些用户凭据保存在设备中。我可以从哪里删除保存的用户并以其他用户身份登录?

windows azure authentication uwp azure-active-directory
1个回答
0
投票

如果您使用

microsoft.identity.client
,那么您可以通过调用方法
GetAccountsAsync()
来访问帐户信息,该方法返回客户端中存储有活动令牌的所有帐户的列表

要访问存储的令牌,您可以调用方法

AcquireTokenSilent()
,然后使用诸如
System.IdentityModel.Tokens.Jwt
之类的库解析令牌字符串以获取实际数据:

var accounts = await app.GetAccountsAsync();

AuthenticationResult result = null;
try
{
     result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                       .ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
    // A MsalUiRequiredException happened on AcquireTokenSilent.
    // This indicates you need to call AcquireTokenInteractive to acquire a token
    Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

    try
    {
        result = await app.AcquireTokenInteractive(scopes)
                          .ExecuteAsync();
    }
    catch (MsalException msalex)
    {
        ResultText.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
    }
}
catch (Exception ex)
{
    ResultText.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
    return;
}

if (result != null)
{
    string accessToken = result.AccessToken;
    // Use the token
}

要删除存储在缓存中的令牌,您可以参考此文档:https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-net-acquire-token-silently#clearing-缓存

© www.soinside.com 2019 - 2024. All rights reserved.