Piranha CMS - UseManager 违反 CORS 政策

Piranha CMS - UseManager breaks CORS policy

我正在将 Piranha CMS (v8.4) 集成到现有 ASP .NET Core 3.1 项目中。我已经让 Piranha 正常工作,但我收到一个异常,表明 CORS 设置不正确。

我已经设法将问题的根源追溯到 Piranha 中间件的配置。 options.UseManager() 行是导致问题的原因,当 options.UseManager() 行被注释掉时,CORS 中间件按预期运行。

         app.UsePiranha(options =>
        {
            options.UseManager();
            options.UseTinyMCE();
            options.UseIdentity();
        });

InvalidOperationException: Endpoint contains CORS metadata, but a middleware was not found that supports CORS. Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).

我的CORS策略是这样配置的。在 UsePiranha 之前或之后调用没有区别:

        // global cors policy
        app.UseCors(x => x
            .AllowAnyMethod()
            .AllowAnyHeader()
            .SetIsOriginAllowed(origin => env.IsDevelopment()) //any origin in dev
            .AllowCredentials());  

`

问题是对 UseManager 的调用调用了 UseRouting。根据例外情况,需要在 UseRouting 之前配置 UseCors。解决方案是在对 UseIdentityUseManager.

的调用之间配置 CORS
 app.UsePiranha(options =>
            {
                options.UseTinyMCE();
                options.UseIdentity();

                app.UseCors(x => x
                   .AllowAnyMethod()
                   .AllowAnyHeader()
                   .SetIsOriginAllowed(origin => env.IsDevelopment()) //any origin in dev
                   .AllowCredentials());

                options.UseManager();
            });