azure - 使用 Azure AD 在 Fiddler 中获取此请求的授权已被拒绝

标签 azure asp.net-web-api azure-active-directory fiddler

我创建了一个 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 时,我看到 audappid按预期(匹配 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中找到App ID URI azure 门户中 api 应用程序的 ,例如 https://xxxxx.onmicrosoft.com/WebApplicationName 。 Web api 将检查访问 token 中的 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/

相关文章:

azure - 是否有可能使用 IAsyncCollector 而不依赖 azure webjobs?

azure - ADAL python 对 Windows 登录用户的支持

c# - Web API OData - 使用 ODataModelBuilder 公开 ComplexType

asp.net-web-api - 带有 Protocol Buffer 的 ASP.NET Web Api - 错误处理

reactjs - 我正在使用 React-adal 进行 Azure AD 单点登录。它的 token 将在 1 小时后过期。有没有办法刷新 session 或延长 session 过期时间

具有 Azure AD 访问 token 的 Azure Function

Azure DevOps 我无法获得 1800 分钟的 MS 托管

configuration - 如何从Azure配置文件获取所有配置设置?

asp.net-web-api - 用于多个应用程序的 OWIN 身份验证服务器

azure - 我怎样才能获得可以传递给azure rest api的不记名 token