【发布时间】:2019-04-16 10:28:07
【问题描述】:
是否可以为当前应用程序获取 jwt 令牌,而不是基于另一个 Azure 应用程序或在整个登录生命周期中保留它?
在我的应用程序中,我使用 Owin 和 OpenIdConnect 向 Azure 验证我的用户。身份验证成功,我收到了来自 Azure 的令牌。
在后面的方法中我需要将令牌作为参数传递,所以现在我将它存储在会话变量中,但这会提前过期并导致一个空变量。
现在我正在尝试使用 ADAL 从 Azure 获取令牌。
string userObjectId = ((ClaimsIdentity)User.Identity).Claims.FirstOrDefault(c => c.ToString() == "http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
AuthenticationContext authContext = new AuthenticationContext(ConfigurationManager.AppSettings["ida:AADInstance"] + ConfigurationManager.AppSettings["ida:TenantId"], new TokenCache());
ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientId"], "client_secret");
var result = authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["ida:ClientId"], credential).Result.AccessToken;
这会导致错误:
{"error":"invalid_grant","error_description":"AADSTS50105: Application > '4337e286-7b84-4377-8843-82ea9504606b' is not assigned to a role for the application '4337e286-7b84-4377-8843-82ea9504606b'}
对于其他应用程序,它与另一个拥有资源权限的应用程序一起设置,但我不想依赖另一个应用程序来获取令牌。
谁有想法?
编辑
我已经听从了@Jean-MarcPrieur 的建议,但是就行了
var accounts = await application.GetAccountsAsync();
它不返回任何帐户,结果为空结果并且没有 accessToken。
【问题讨论】: