CORS 适用于访问令牌,但不适用于 Web 中的刷新令牌 Api 2
CORS works for access token but not for refresh token in Web Api 2
我有一个 Web api 2 应用程序,我使用 angularjs 客户端调用它。 Web api 应用程序能够颁发访问令牌和刷新令牌以进行身份验证。
在 "GrantResourceOwnersCredentials" 方法中有以下几行,CORS 工作正常,允许发布访问令牌:
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
if (allowedOrigin == null) allowedOrigin = "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
但是,当我尝试通过 angularjs 应用程序发出刷新令牌时,我在控制台中收到了这个好旧的错误:
OPTIONS http://localhost:65141/token
(index):1 XMLHttpRequest cannot load http://localhost:65141/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56815' is therefore not allowed access. The response had HTTP status code 400.
我在想,由于访问令牌的发布很好,刷新令牌也使用相同的端点发布,我应该怎么做才能解决这个问题?
顺便说一下,angular 代码没问题。我禁用了 google chrome 网络安全,然后一切正常!非常感谢任何帮助!
在搜索了整个该死的互联网后,我找到了解决问题的办法。将此代码添加到授权提供程序将解决问题:
public override Task MatchEndpoint(OAuthMatchEndpointContext context)
{
if (context.IsTokenEndpoint && context.Request.Method == "OPTIONS")
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "authorization" });
context.RequestCompleted();
return Task.FromResult(0);
}
return base.MatchEndpoint(context);
}
我有一个 Web api 2 应用程序,我使用 angularjs 客户端调用它。 Web api 应用程序能够颁发访问令牌和刷新令牌以进行身份验证。
在 "GrantResourceOwnersCredentials" 方法中有以下几行,CORS 工作正常,允许发布访问令牌:
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
if (allowedOrigin == null) allowedOrigin = "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
但是,当我尝试通过 angularjs 应用程序发出刷新令牌时,我在控制台中收到了这个好旧的错误:
OPTIONS http://localhost:65141/token
(index):1 XMLHttpRequest cannot load http://localhost:65141/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56815' is therefore not allowed access. The response had HTTP status code 400.
我在想,由于访问令牌的发布很好,刷新令牌也使用相同的端点发布,我应该怎么做才能解决这个问题?
顺便说一下,angular 代码没问题。我禁用了 google chrome 网络安全,然后一切正常!非常感谢任何帮助!
在搜索了整个该死的互联网后,我找到了解决问题的办法。将此代码添加到授权提供程序将解决问题:
public override Task MatchEndpoint(OAuthMatchEndpointContext context)
{
if (context.IsTokenEndpoint && context.Request.Method == "OPTIONS")
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "authorization" });
context.RequestCompleted();
return Task.FromResult(0);
}
return base.MatchEndpoint(context);
}