我正在尝试将Microsoft sso与Xamarin.Forms应用程序集成。
我正在使用Microsoft.Identity.Client 4.7.1
我努力在同一台设备上使用不同的帐户登录,因为无论我做什么,似乎总是会选择第一个帐户。
以下是用于登录和退出的代码:
private IPublicClientApplication _publicClientApplication;
public AuthService()
{
_publicClientApplication = PublicClientApplicationBuilder.Create(Constants.MicrosoftAuthConstants.ClientId.Value)
.WithAdfsAuthority(Constants.MicrosoftAuthConstants.Authority.Value)
.WithRedirectUri(Constants.MicrosoftAuthConstants.RedirectUri.Value)
.Build();
}
public async Task<string> SignInAsync()
{
var authScopes = Constants.MicrosoftAuthConstants.Scopes.Value;
AuthenticationResult authResult;
try
{
// call to _publicClientApplication.AcquireTokenSilent
authResult = await GetAuthResultSilentlyAsync();
}
catch (MsalUiRequiredException)
{
authResult = await _publicClientApplication.AcquireTokenInteractive(authScopes)
.WithParentActivityOrWindow(App.ParentWindow)
.ExecuteAsync();
}
return authResult.AccessToken;
}
private async Task<IAccount> GetCachedAccountAsync() => (await _publicClientApplication.GetAccountsAsync()).FirstOrDefault();
public async Task SignOutAsync()
{
var firstCachedAccount = await GetCachedAccountAsync();
await _publicClientApplication.RemoveAsync(firstCachedAccount);
}
一种解决方法是使用Prompt.ForceLogin
,但是如果您每次必须键入凭据,那么sso的意义是什么。
答案 0 :(得分:1)
代码行await _publicClientApplication.RemoveAsync(firstCachedAccount);
可以从缓存中删除用户,它没有实现签出方法。因此,您需要通过下面的api手动注销:
https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=https://localhost/myapp/