【发布时间】:2017-12-08 07:22:25
【问题描述】:
我们在 Azure 订阅中有一个应用注册,需要访问 Graph,在“所需权限”部分下设置。
我们的 Angular 应用程序正在启动针对 AAD 的身份验证,接收 JWT,将其附加到 Header 中的所有请求中,并将其发送到我们的 WebApi,在那里它正在被成功验证......太棒了!
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERw...
现在,我还想从 WebApi 向 Graph 发出请求,考虑到我拥有经过验证的令牌,我将通过 Authorization 标头将其传递给 Graph REST API。
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
但是,我得到了 401(未经授权)结果。为什么?显然我遗漏了一块拼图,但我不知道是什么。
我假设通过在 Azure 应用注册中设置 Graph 权限,当它向我们的 Angular 应用发送令牌时,它对 AAD 和 Graph 都有效......但似乎情况并非如此。
我的 JWT 如下所示。请注意,“aud”仅显示我的应用注册 ID,而不显示 Graph(如果这是问题所在,我该如何解决?)
{
"aud": "<application id here>",
"iss": "https://sts.windows.net/<tenant id here>/",
"iat": 1499121655,
"nbf": 1499121655,
"exp": 1499125555,
"aio": "ASQA2/8DAAAACuKzzzni2uaVoaIb9yJa4j3XuG0O+9cQQQlnqXl8Sr0=",
"amr": [
"pwd",
"mfa"
],
"family_name": "Smith",
"given_name": "John",
"name": "John Smith",
"nonce": "<nonce here>",
"platf": "3",
"pwd_exp": "941678",
"sub": "<sub here>",
"tid": "<tenant id here>",
"unique_name": "jsmith@smithy.com",
"upn": "jsmith@smithy.com",
"ver": "1.0"
}
编辑
我现在已经实现了如南宇描述的代表代码,但我仍然收到 401。前端令牌正在 WebAPI 中进行验证,然后创建对 Graph 的请求。
string authString = ConfigurationManager.AppSettings["ida:Authority"];
string clientId = ConfigurationManager.AppSettings["ida:Audience"];
string clientSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
string graphResourceId = ConfigurationManager.AppSettings["ida:GraphResourceId"];
AuthenticationContext authenticationContext = new AuthenticationContext(authString);
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
var bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as System.IdentityModel.Tokens.BootstrapContext;
string userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
string userAccessToken = bootstrapContext.Token;
UserAssertion userAssertion = new UserAssertion(bootstrapContext.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationResult result = await authenticationContext.AcquireTokenAsync(graphResourceId, clientCred, userAssertion);
这可以正常工作,因为返回的令牌现在具有登录用户的图表“aud”和“scp”:
"aud": "https://graph.windows.net",
"scp": "User.Read User.ReadBasic.All",
"unique_name": "jsmith@smithy.com",
"upn": "jsmith@smithy.com",
为什么我在尝试调用 /me Graph API 时仍然未经授权?
【问题讨论】:
标签: c# asp.net azure azure-active-directory microsoft-graph-api