【问题标题】:Angular4 and Azure AD: impossible to catch data from the API : 401 UnauthorizedAngular4 和 Azure AD:无法从 API 捕获数据:401 Unauthorized
【发布时间】:2017-07-19 17:16:56
【问题描述】:

我在构建 Angular4/Angular2 应用和 Azure AD 身份验证时遇到问题。

首先我使用 ADAL-Angular4 (https://www.npmjs.com/package/adal-angular4) 并且 angular 服务器在本地运行。 在 Azure 上,我有一个带有我想要保护的 API 的 Web 应用程序。 所以我创建了一个 Active Directory 应用程序,并将其与网络应用程序 (https://img15.hostingpics.net/pics/381684Capturedcran20170719094203.png) 链接。

第一个身份验证运行良好(访问网络应用程序)。 但是在调用 API 时(仅限于经过身份验证的用户),我每次都得到以下响应

401 error : Unauthorized

似乎我无法访问资源,因为我没有授予的访问权限。但我的帐户是所有应用程序的管理员,我在 Azure 上的所有位置都可以访问。

我在想 ADAL 并没有捕捉到所有的请求,但即使我放了

let head = new Headers({Authorization: 'Bearer '+this.service.userInfo.token})
head.append( 'Host' , 'xxx.azurewebsites.net' )
return this._http.get('https://xxx.azurewebsites.net/api/getColumnsName/'{headers: head})

在我的请求服务中,它不起作用,我得到同样的错误......

这是我的 app.component 上的 Adal 配置

const config = {
  tenant: 'XXXXXX.onmicrosoft.com',
  clientId: 'XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX' //authApp ID
}                                   

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html'
})

export class AppComponent { 
     constructor(private service: Adal4Service) {      // <-- ADD
    this.service.init(config);                      // <-- ADD
  }                              
}

编辑:

当我进行身份验证时,我会得到这些参数(出于安全原因,有些参数已被隐藏并且令牌已被剪切)

[Log] username toto@hotmail.fr (main.bundle.js, line 2436)
[Log] authenticated: true (main.bundle.js, line 2437)
[Log] name: toto (main.bundle.js, line 2438)
[Log] token: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Il

服务器发送的响应是这个(来自天蓝色日志):

</head> 
<body> 
<div id="content"> 
<div class="content-container"> 
<h3>HTTP Error 401.0 - Unauthorized</h3> 
<h4>You do not have permission to view this directory or page.</h4> 
</div> 
<div class="content-container"> 
<fieldset><h4>Most likely causes:</h4> 
<ul>    <li>The authenticated user does not have access to a resource needed to process the request.</li> </ul> 
</fieldset> 
</div> 
<div class="content-container"> 
<fieldset><h4>Things you can try:</h4> 
<ul>    <li>Create a tracing rule to track failed requests for this HTTP status code. For more information about creating a tracing rule for failed requests, click <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>. </li> </ul> 
</fieldset> 
</div> 

<div class="content-container"> 
<fieldset><h4>Detailed Error Information:</h4> 
<div id="details-left"> 
<table border="0" cellpadding="0" cellspacing="0"> 
<tr class="alt"><th>Module</th><td>&nbsp;&nbsp;&nbsp;iisnode</td></tr> 
<tr><th>Notification</th><td>&nbsp;&nbsp;&nbsp;ExecuteRequestHandler</td></tr> 
<tr class="alt"><th>Handler</th><td>&nbsp;&nbsp;&nbsp;iisnode</td></tr> 
<tr><th>Error Code</th><td>&nbsp;&nbsp;&nbsp;0x00000000</td></tr> 

</table> 
</div> 
<div id="details-right"> 
<table border="0" cellpadding="0" cellspacing="0"> 
<tr class="alt"><th>Requested URL</th><td>&nbsp;&nbsp;&nbsp;https://XXXXX:80/app.js</td></tr> 
<tr><th>Physical Path</th><td>&nbsp;&nbsp;&nbsp;D:\home\site\wwwroot\app.js</td></tr> 
<tr class="alt"><th>Logon Method</th><td>&nbsp;&nbsp;&nbsp;aad</td></tr> 
<tr><th>Logon User</th><td>&nbsp;&nbsp;&nbsp;toto@hotmail.fr</td></tr> 

</table> 
<div class="clear"></div> 
</div> 
</fieldset> 
</div> 

<div class="content-container"> 
<fieldset><h4>More Information:</h4> 
This is the generic Access Denied error returned by IIS. Typically, there is a substatus code associated with this error that describes why the server denied the request. Check the IIS Log file to determine whether a substatus code is associated with this failure. 
<p><a href="http://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=401,0,0x00000000,9200">View more information &raquo;</a></p> 
<p>Microsoft Knowledge Base Articles:</p> 


</fieldset> 
</div> 
</div> 
</body> 
</html> 

编辑2:

我使用 azure “easy API”,并将 get/post/request 权限设置为“Authenticated Users Only”

【问题讨论】:

  • 缺少逗号还是打错字? URL 后面应该有一个,因为 headers 是http.get() 的参数。我们有一个类似的解决方案并且正在运行,唯一的其他区别(您的代码视图有限)是我们附加了一个 'Accept' 标头以及 'application/json' 的值。
  • 逗号出现在我的代码中,堆栈编辑器把它拿走我知道为什么。即使我添加了您的线路,它也会响应我的未经授权。也许我错过了配置我的 azure add 和 web 应用程序? @DSN
  • 应该有一个更长的消息和401 error。你能分享那个消息吗?您是否还可以共享您的访问令牌的内容(删除任何敏感数据)?
  • 我已经添加了@ShawnTabrizi

标签: angular azure http azure-active-directory


【解决方案1】:

您似乎已经通过 IIS 或其他类型的机制(不确定是哪种)在 Web API 上配置了身份验证。但一般来说,如果您想验证由 Azure AD 颁发并由 ADAL 库获取的令牌,您可以在您的 ASP.NET 应用程序中使用 WindowsAzureActiveDirectoryBearerAuthenticationMiddleware 来处理令牌和验证传入呼叫。 Here's a basic sample 展示了如何使用这个中间件。或者,您也可以使用 System.IdentityModel.Tokens.Jwt 库来验证来自 Azure AD 的令牌,就像在 this code sample 中所做的那样。

【讨论】:

  • 感谢您的回答,但我忘了说我直接使用 Azure Easy API,所以我没有(通常)做这种事情(我做了一个编辑) @dstrockis
  • @Antoine 根据该图,您可以保护 Web API,但允许匿名请求。看来你还保护了项目中的web API,你介意检查一下并分享代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2020-07-25
  • 2016-04-05
  • 1970-01-01
相关资源
最近更新 更多