OWIN OpenId" insufficient_scope"使用OpenIDConnect令牌进行CORS请求时

时间:2014-10-27 23:08:21

标签: angularjs asp.net-web-api cors owin thinktecture-ident-server

在ASP.NET MVC WebAPI中使用OWIN OpenID连接令牌启用CORS需要做什么?

我正在使用Thinktecture Identity Server v3和隐式流程。

我已经在https://b3ncr.comms/托管的AngularJS应用程序,https://b3ncr.auth/上的IdentityServer v3和https://b3ncr.comms.api/上的api

我的API配置如下。

app.UseIdentityServerJwt(new JwtTokenValidationOptions
{
    Authority = "https://b3ncr.auth:44340/identity"
});

app.RequireScopes("sampleApi");

我成功登录并获得包含以下参数的令牌。

https://b3ncr.auth:44340/identity/connect/authorize?response_type=id_token token
    &client_id=Implicit
    &scope=openid profile sampleApi
    &redirect_uri=https://b3ncr.comms:44341/logon
    &nonce=BLAH

按预期返回一个令牌,我将其解压缩并尝试调用我的API。当我尝试使用令牌AngularJS调用api时,发出以下HTTP请求:

Request URL:https://b3ncr.comms.api:44342/api/Identity
Request Method:OPTIONS

Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:b3ncr.comms.api:44342
Origin:https://b3ncr.comms:44341
Pragma:no-cache
Referer:https://b3ncr.comms:44341/

然后我的API会返回以下标题:

Access-Control-Allow-Headers:Content-Type, Authorization
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:*
Content-Length:0
Date:Mon, 27 Oct 2014 22:48:24 GMT
Server:Microsoft-IIS/8.5
WWW-Authenticate:Bearer error="insufficient_scope"
X-Powered-By:ASP.NET

对我来说,看起来像OWIN中间件拒绝该请求,因为该令牌不包含API所需的范围以及我使用我的授权参数请求的范围。但是,如果我直接使用令牌,例如在Fiddler中它可以很好地工作。

编辑:我没有在请求中包含{ withCredentials: true }配置选项。我已添加它但仍然遇到同样的问题。

再次编辑:事实证明我不应该使用withCredentials作为授权标题,这只是用于使用cookie。

0 个答案:

没有答案