我创建了一个 ASP.Net Web API (.Net Framework) 应用程序,并使用“工作或学校帐户”作为身份验证类型。这会自动在我的 Azure 订阅中注册此 API 应用程序,我可以在 "App Registrations" 下看到它。我可以看到主页网址指向 localhost address 。我可以看到 API 正在本地主机地址上本地启动。然后,我启动 Fiddler 以从 Azure AD 获取访问 token 。我对端点 https://login.microsoftonline.com/<mytenant>.onmicrosoft.com/oauth2/token
的 POST 请求。有以下4个参数
grant_type=client_credentials
&client_id=<appid from Azure AD Portal>
&client_secret=<secret from Azure AD Portal>
&resource=<appid from Azure AD Portal>
我拿回了一个 token 。当我解码这个 token 时,我看到 aud
和appid
按预期(匹配 Azure AD 中的 appid)。我使用此 token 作为不记名 token 来通过添加 Authorization: Bearer <mytoken>
来调用 API 调用GET 请求中的 header https://localhost:44374/api/values
。但是,对我的 API 的此 GET 调用返回给我 {"Message":"Authorization has been denied for this request."}
错误信息。
我错过了什么?
最佳答案
获取 token 时,应使用App ID URI
作为resource
值,您可以在Properties中找到
,例如 https://xxxxx.onmicrosoft.com/WebApplicationName 。 Web api 将检查访问 token 中的 App ID URI
azure 门户中 api 应用程序的 aud
声明是否与您在 web.config 中设置的声明匹配:
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters {
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
},
});
web.config 中的 ida:Audience
值是允许的受众。
关于azure - 使用 Azure AD 在 Fiddler 中获取此请求的授权已被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45139729/