Ant*_*h12 10 .net c# asp.net-web-api azure-active-directory
我有一个在Azure网站上运行的标准Web API,启用了Azure AD身份验证,当在浏览器中浏览到API时,我可以通过浏览器登录并获得对API的访问权限.
但是,WPF桌面应用程序在提交请求时收到了未经授权的响应:
var authContext = new AuthenticationContext(authority, new FileCache());
var accessToken = await authContext.AcquireTokenAsync(apiResourceid, clientId, redirectUri,
new PlatformParameters(PromptBehavior.Auto));
// accessToken is valid
var apiUrl = "https://example.azurewebsites.net/api/list";
var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
var response = await httpClient.SendAsync(request);
Run Code Online (Sandbox Code Playgroud)
验证成功,我可以在调试时看到用户信息.
我无法访问Azure帐户,但我确信服务AD应用程序已正确配置以允许访问客户端AD应用程序,因为在备用帐户(未正确配置)上进行测试时AuthenticationContext.AcquireTokenAsync
方法失败.
我确实注意到它 - (时间当然是UTC)AuthenticationResult.ExpiresOn
总是在过去,但是如果这是未来的日期,那么就没有办法扩展它了吗?
请求:
GET https://example.azure
websites.net/api/categorisation HTTP/1.1
Authorization: Bearer eyJ0eXAiO...
Host: example.azurewebsites.net
Run Code Online (Sandbox Code Playgroud)
响应:
HTTP/1.1 401 Unauthorized
Content-Length: 58
Content-Type: text/html
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="example.azurewebsites.net"
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=e35f2977dba55e6708887e762940f75c2a0fcb0a9df4e1cbe0d3f10a614c59b8;Path=/;Domain=example.azurewebsites.net
Date: Fri, 08 Jul 2016 07:51:13 GMT
You do not have permission to view this directory or page.
Run Code Online (Sandbox Code Playgroud)
更新:
我已经在我有权访问的Azure帐户中重新创建了环境,并且仍然收到了未经授权的响应(在浏览器中正常工作).
问题似乎与 Azure 网站中的“身份验证/授权”选项有关,启用时 Web Api 将不接受使用身份验证标头的请求。禁用该选项并将 Owin 库与 Azure AD 一起使用提供了所需的解决方案。
归档时间: |
|
查看次数: |
11316 次 |
最近记录: |