C# 如何使用AcquireTokenSilentAsync对用户进行身份验证?
我正试图通过编程方式从Azure AD加入计算机授权Azure应用程序 如果我转到Internet Explorer中的应用程序URL,它可以验证登录的用户帐户 我当前的代码如下所示: using Microsoft.IdentityModel.Clients.ActiveDirectory; AuthenticationContext context = new AuthenticationContext("https://login.microsoftonline.com/TENANTGUID"); Uri uri = new Uri("urn:ietf:wg:oauth:2.0:oob"); var pparams = new PlatformParameters(PromptBehavior.Auto, null); AuthenticationResult result = await context.AcquireTokenAsync("https://graph.windows.net", "1950a258-227b-4e31-a9cf-717495945fc2", uri, pparams); 此调用成功,但我想为当前登录的用户获取令牌 AcquireTokenAsync调用的前两个参数是resource和clientid 我可以获取我想要访问的应用程序的主页url和应用程序id,但找不到两者的组合C# 如何使用AcquireTokenSilentAsync对用户进行身份验证?,c#,azure,adal,msal,C#,Azure,Adal,Msal,我正试图通过编程方式从Azure AD加入计算机授权Azure应用程序 如果我转到Internet Explorer中的应用程序URL,它可以验证登录的用户帐户 我当前的代码如下所示: using Microsoft.IdentityModel.Clients.ActiveDirectory; AuthenticationContext context = new AuthenticationContext("https://login.microsoftonline.com/TENANTGU
我应该向此函数传递哪些参数以静默方式验证登录的用户,并获取可在随后调用应用程序时使用的授权标头?我建议您现在为域或AAD加入的计算机提供MSAL.NET: 代码类似于:
static async Task GetATokenForGraph()
{
string tenant = "contoso.com" // can also be a GUID or organizations for multi-tenant
string authority = $"https://login.microsoftonline.com/{tenant}";
string[] scopes = new string[] { "user.read" };
PublicClientApplication app = new PublicClientApplication(clientId, authority);
var accounts = await app.GetAccountsAsync();
AuthenticationResult result=null;
if (accounts.Any())
{
result = await app.AcquireTokenSilentAsync(scopes, accounts.FirstOrDefault());
}
else
{
try
{
result = await app.AcquireTokenByIntegratedWindowsAuthAsync(scopes);
}
catch (MsalUiRequiredException ex)
{
// For details see the article
你想要什么https://login.microsoftonline.com/$TenantID/oauth2/token该资源是您想要令牌的APO的标识符。https://graph.windows.net 是Azure AD Graph API的标识符。https://graph.microsoft.com 是Microsoft Graph API的标识符。这完全取决于令牌缓存中是否有一些凭据可以工作。如果我改为使用oauth2/token,则会出现错误AADSTS90002:找不到租户令牌。如果租户没有活动订阅,则可能发生这种情况。请与您的订阅管理员联系。如果我尝试使用应用程序clientid代替graph.windows.net,则会出现如下错误:AADSS50011:请求中指定的答复url与为应用程序配置的答复url不匹配:“clientid”。AADSTS50001:在名为TENANTGUIDThanks的租户中未找到名为CLIENTNAME的应用程序感谢您的帮助当我尝试上述操作时,首先未找到任何帐户,然后AcquireTokenByIntegratedWindowsAuthAsync失败,错误为Win32Exception:系统无法联系域控制器来服务身份验证请求。请稍后重试如果我尝试使用AcquireTokenAsync而不是AcquireTokenByIntegratedAuthAsync,则会弹出一个登录windows,其中包含登录的用户Id,但选中该Id时会发生以下错误:AADSS50011:请求中指定的回复url与为应用程序配置的回复url不匹配:“CLIENTID”。@Paul海豚如果系统无法联系域控制器,这可能是因为您的计算机未加入域,或者您的VPN因AcquireTokenAsync而关闭,这意味着您需要检查应用注册中的urn:ietf:wg:oauth:2.0:oob回复URI。请看