用于客户端应用程序的 Azure Active Directory 静默 SSO(令牌检索)



我有一个客户端应用程序,我想检查 azure ad 令牌声明。通过客户端应用程序,我说的是运行在用户上下文中的Windows 10桌面或UWP应用程序上运行的可执行文件。

我试过:

var authenticationContext =
new AuthenticationContext("https://login.microsoftonline.com/common");
var userCredential = new UserCredential();
var result = await authenticationContext
.AcquireTokenAsync("https://graph.microsoft.com/.default",
"https://mytenant.onmicrosoft.com/myguid",
userCredential);

此操作失败,并显示password_required_for_managed_user.

可用的文档声称此方法使用 Kerberos,如果为 true,则 Azure ADd 不支持 Kerberos,因此它将失败。

var _publicClientApp = new PublicClientApplication(clientId);
var user = new ApplicationUser {
DisplayableId = upn,
Identifier = Guid.NewGuid().ToString()
// other fields can be null
};
var authenticationResult =
await _publicClientApp.AcquireTokenSilentAsync(_scopes, user);

MsalUiRequiredException (no token in cache).

这不再使用 ADAL,它使用不同的AcquireTokenSilentAsync实现:

var authContext =
new AuthenticationContext("https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache.DefaultShared);
var result =
await authContext.AcquireTokenSilentAsync("https://graph.microsoft.com/.default", upn);

这将返回相同的MsalUiRequiredException

我不知所措。对于 Web 应用程序,浏览器缓存中将有一个 cookie,我可以使用标准运行时执行静默 SSO。

我是否必须在应用程序中嵌入 Web 服务器才能执行相同的操作?

AcquireTokenSilentAsync使用现有refresh_token来获取更新的access_token。由于您尚未获取令牌,因此它失败(因此出现"缓存中没有令牌"错误(。

您需要提供回退来处理没有有效refresh_token可用的情况:

AuthenticationResult authResult = null;
string[] scopes = new string[] { "user.read", "mail.read" };
try 
{
authResult = await App
.PublicClientApp
.AcquireTokenSilentAsync(
scopes, 
App.PublicClientApp.Users.FirstOrDefault());
} 
catch (MsalUiRequiredException ex) 
{
// MSAL couldn't get the token silently, so go through the interactive process
authResult = await App
.PublicClientApp
.AcquireTokenAsync(scopes);
}

还有一些演练可用于为经典应用和 UWP 应用设置此设置:

  • 视窗桌面
  • 通用 Windows Platform (UWP(

最新更新