以编程方式注销 Azure 身份应用程序

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

以前我可以执行此操作来退出我的应用程序:

public async Task<bool> SignOut()
{
    var accounts = await PublicClientApp.GetAccountsAsync();
    if (accounts.Any())
    {
        try
        {
            await PublicClientApp.RemoveAsync(accounts.FirstOrDefault());
            return true;
        }
        catch (MsalException ex)
        {
            _logger.Error(ex);
            Console.WriteLine("SignOut: See error log.");
            return false;
        }
    }

    return false;
}

但我现在正在使用

DeviceCodeCredential.AuthenticationRecord
。已经没有
account
了。

用户触发注销的最简单方法是什么?

c# microsoft-graph-api refresh-token azure-identity single-sign-out
1个回答
0
投票

要以编程方式注销 Azure 身份应用程序,您可以使用以下端点:

https://login.microsoftonline.com/TenantID/oauth2/logout?client_id=ClientID&post_logout_redirect_uri=RedirectURL

当您配置了多租户应用程序时,您需要将 TenantID 作为

common

传递
https://login.microsoftonline.com/common/oauth2/logout?client_id=ClientID&post_logout_redirect_uri=https://jwt.ms

enter image description here

enter image description here

即使在注销并且不删除令牌文件后,您也会收到此错误“您已注销您的帐户”。而且我仍然可以登录 Outlook。所以这就是一件事。但我的令牌仍然有效,因此当我第二次运行我的应用程序时,它仍然有效”,因为令牌有效

此问题通常发生在令牌仍然有效的情况下,因此您需要清除令牌缓存,即当用户注销时,您应该从令牌缓存中删除与该用户关联的令牌必须清除所有与用户相关的令牌缓存带有注销 URL,然后就可以了。

因此,首先尝试删除令牌文件并注销用户,用户将成功注销。

public static void SignOut()  
{  
// Ensure settings isn't null  
_ = _settings ?? throw new System.NullReferenceException("The variable '_settings' should not be null");  
  
// Delete the token cache file.  
if (Path.Exists(Program.OutlookOptions.TokenCachePath))  
{  
File.Delete(Program.OutlookOptions.TokenCachePath);  
}  
  
// Logout  
string url = string.Format("https://login.microsoftonline.com/common/oauth2/logout?client_id={0}", _settings.ClientId);  
System.Diagnostics.Process.Start("explorer", url);  
}  
© www.soinside.com 2019 - 2024. All rights reserved.