asp.net - CORS 不适用于 Thinktecture.IdentityModel.45/ token

标签 asp.net asp.net-web-api cors thinktecture-ident-model

(我很抱歉在此处和 github 发布此问题,但我确实需要这方面的帮助..)

使用 Thinktecture.IdentityModel.45 进行身份验证时,我无法启用 CORS (System.Web.Http.Cors)。

我的 token 端点有问题,我使用默认的“/token”。

发生的情况是,当(从 JavaScript 客户端)调用/token 时,浏览器发出 2 个调用,即“OPTION”调用和“/token”资源的“GET”调用。我使用一个简单的 PolicyProviderFactory 只是为了能够检查请求是否获得正确的 CORS 策略并且它只是返回

        new EnableCorsAttribute("*", "*", "*");

第一个 OPTION 调用有效,并且 GetCorsPolicyProvider 被命中。另一个调用有效,但 GetCorsPolicyProvider 没有被命中,结果是服务器返回 200 OK 以及所有正确的 header 等,但是内容响应为空。

我需要在路由配置中为 token 资源添加以下内容,否则我会得到“NOT FOUND”。

        config.Routes.MapHttpRoute(
            name: "SecurityToken",
            routeTemplate: "api/token");

对于所有其他请求,OPTIONS 和 GET 都会调用 CORS 策略提供程序,并且一切正常。我已调试到 thinktecture.identitymodel 和正确的响应,返回了正确的 token 。

/token 资源的 GET 方法不会调用 CORS 策略提供程序。但为什么呢? CorsMessageHandler 已加载但从未被调用。如何判断请求是否会触发 CORS/CorsMessageHandler?

诊断跟踪:

w3wp.exe Information: 0 : Request, Method=OPTIONS, Url=https://localhost/myapp/api/token, Message='https://localhost/myapp/api/token'
w3wp.exe Information: 0 : Message='CorsPolicyProvider selected: 'System.Web.Http.Cors.EnableCorsAttribute'', Operation=MyPolicyProviderFactory.GetCorsPolicyProvider
w3wp.exe Information: 0 : Message='CorsPolicy selected: 'AllowAnyHeader: True, AllowAnyMethod: True, AllowAnyOrigin: True, PreflightMaxAge: null, SupportsCredentials: True, Origins: {}, Methods: {}, Headers: {}, ExposedHeaders: {}'', Operation=EnableCorsAttribute.GetCorsPolicyAsync
w3wp.exe Information: 0 : Message='CorsResult returned: 'IsValid: True, AllowCredentials: True, PreflightMaxAge: null, AllowOrigin: https://localhost:4433, AllowExposedHeaders: {}, AllowHeaders: {access-key,authorization}, AllowMethods: {GET}, ErrorMessages: {}'', Operation=CorsEngine.EvaluatePolicy
w3wp.exe Information: 0 : Operation=CorsMessageHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Operation=AuthenticationHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Response, Status=200 (OK), Method=OPTIONS, Url=https://localhost/myapp/api/token, Message='Content-type='none', content-length=unknown'
w3wp.exe Information: 0 : Request, Method=GET, Url=https://localhost/myapp/api/token, Message='https://localhost/myapp/api/token'
w3wp.exe Information: 0 : Operation=AuthenticationHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Response, Status=200 (OK), Method=GET, Url=https://localhost/myapp/api/token, Message='Content-type='application/json; charset=utf-8', content-length=851'

身份验证配置如下所示:

var authentication = new AuthenticationConfiguration
    {
         ClaimsAuthenticationManager = new ClaimsTransformer(),
         RequireSsl = false, 
         EnableSessionToken = true,
         SendWwwAuthenticateResponseHeaders = true
     };

     var accessKeyHandler = new SimpleSecurityTokenHandler(
            "accessKey", 
            AccessKey.Validate);

        authentication.AddAccessKey(
            accessKeyHandler,
            AuthenticationOptions.ForHeader("access-key"));

        authentication.AddBasicAuthentication(UserCredentials.Validate, retainPassword: true);
        PassiveSessionConfiguration.ConfigureMackineKeyProtectionForSessionTokens();

最佳答案

为了让 CORS 与 OWIN token 端点完美配合,我必须安装 Microsoft.Owin.Cors Nuget 包,然后添加以下代码行:

Startup.Auth.cs

using Microsoft.Owin.Cors;
// ...

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        // ...
        app.UseCors(CorsOptions.AllowAll);

添加包并在代码中配置 CORS 后,效果很好。

关于asp.net - CORS 不适用于 Thinktecture.IdentityModel.45/ token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21255325/

相关文章:

c# - 创建将由桌面应用程序和网站使用的对象模型时,我应该考虑哪些特殊注意事项?

c# - Blazor WebAssembly 应用程序 - 来自浏览器的 API 调用 - 抱歉,此地址没有任何内容

javascript - 一劳永逸 : Is there or is there not a way to manipulate an image from another domain using IE9+

javascript - asp.net - 从弹出窗口显示弹出窗口

c# - 使用数据库使用 asp.net 构建菜单

c# - 为 ASP.net WebAPI 自动生成文档

spring - 在 Spring Boot Actuator 中为/health 端点启用 CORS

java - 如何使用 Jersey 中的 ContainerResponseFilter 为 CORS 允许多个域。?

javascript - 如何在单个复选框事件上选中/取消选中所有 asp.net 复选框?

asp.net - 使用 XSP/Mono 托管时,ASP .Net WebAPI 不适用于 Owin