此请求已拒绝使用AAD -Authorization的安全Web API

时间:2018-06-06 21:02:19

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

我使用angularJS网络应用程序登录azure =>这部分是有效的。

但是当我尝试访问我的网络应用程序中的授权控制器时,我收到“授权被拒绝”。授权持有者令牌已发送到Web API

我的Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters {
                     ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                },
            });
    }

ApiController

 [Authorize]
public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

错误:

<Error>
<script/>
  <Message>Authorization has been denied for this request.</Message>
</Error>

响应标题:

cache-control: no-cache
content-length: 81
content-type: application/xml; charset=utf-8
date: Thu, 07 Jun 2018 15:04:06 GMT
expires: -1
pragma: no-cache
server: Microsoft-IIS/10.0
status: 401
www-authenticate: Bearer
x-aspnet-version: 4.0.30319
x-powered-by: ASP.NET
x-sourcefiles: =?UTF-8?B? 
QzpcVXNlcnNcanF1aW5vbmVzViQXBwbGljYXRp 
b245XGFwaVx2YWx1ZXM=?=

1 个答案:

答案 0 :(得分:1)

AFAIK,我们将利用Angular JS应用程序中的adal.jsadal-angular.js来验证用户并在客户端获取令牌。您可以按照教程Azure AD AngularJS getting startedIntegrating Azure AD into an AngularJS single page app详细说明此问题。

  

但是当我尝试访问我的网络应用程序中的授权控制器时,我收到了#34;授权已被拒绝&#34;。授权持有者令牌已发送到Web API。

如果您手动启用中间件来验证令牌,则需要确保已正确配置WindowsAzureActiveDirectoryBearerAuthenticationOptions.AudienceTokenValidationParameters.AllowedAudience(s),并将其与aud属性进行比较。传入的JWT令牌。您可以在浏览应用程序时按F12并跟踪网络或使用Fiddler捕获您的持有者令牌,然后使用https://jwt.io/解码您的令牌。

此外,如果您为后端Web应用程序使用内置Authentication and authorization in Azure App Service,则需要在身份验证/授权下正确配置AD身份验证的客户端ID或允许的令牌听众您的应用服务应用的刀片。