【发布时间】:2018-08-02 20:27:55
【问题描述】:
在我的开发环境中,一切正常。但是,要进行暂存,现在我们肯定会遇到不同的域和 CORS 问题,我已经完全解决了这些问题,但可能存在一个问题。
关于我的 API 的 CORS 配置,我正在使用 Microsoft.AspNetCore.Cors Nuget 包,因为我找不到使用 ServiceStack CORS 功能将某些域列入白名单的方法,并且我阅读了 ServiceStack 文档...我现在知道当我实例化ServiceStack 特性有一个重载构造函数:
CorsFeature(ICollection<string> allowOriginWhitelist, string allowedMethods = "GET, POST, PUT, DELETE, PATCH, OPTIONS", string allowedHeaders = "Content-Type", bool allowCredentials = false, string exposeHeaders = null, int? maxAge = null);
无论如何,所以我使用Microsoft.AspNetCore.Cors。在我的暂存环境中正确配置了 CORS 以满足我的需求,我从我的 ServiceStack 身份验证 API 获得了成功的身份验证响应,如下所示:
{
"UserId": "1",
"SessionId": "V8wCKxOooCwLsQ1cn2jp",
"DisplayName": "foo",
"ReferrerUrl": "mydomain",
"ResponseStatus": {}
}
就像这个 ServiceStack user was experiencing。在这个引用的链接中,我看到@mythz 说,“提供者应该是”凭据“。这让我想知道我是否有 CORS 问题,因为我使用的是 Microsoft.AspNetCore.Cors 而不是 Access-Control-Allow-Credentials 为假的 ServiceStack CORS 功能,并且ServiceStack 代码正在检查这个值,不会返回熊令牌。这是我使用Microsoft.AspNetCore.Cors的配置
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<DbSettings>(options => Configuration.GetSection("DbSettings").Bind(options));
services.AddTransient<ITsoContext, TsoContext>();
services.AddTransient<AuthService>();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.SetIsOriginAllowed(CorsHelper.IsOriginAllowed)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (EnvironmentHelper.IsDevelopment(env.EnvironmentName) || EnvironmentHelper.IsLocalhost(env.EnvironmentName))
{
app.UseDeveloperExceptionPage();
}
app.UseCors("CorsPolicy");
app.UseServiceStack(new AppHost(Configuration, env));
}
}
所以在我的 AUTH API 中禁用我的 CORS 配置,而是在 AppHost.Configure 方法中使用 ServiceStack CORS:
var corsFeature = new CorsFeature("*", "GET, POST, PUT, DELETE, PATCH, OPTIONS", "Content-Type", true);
Plugins.Add(corsFeature);
最后一个布尔参数将allowCredentials 设置为true。
但是我们有 Preflight Request CORS 问题:
所以当我将我的域列入白名单时,将 allowCredentials 设置为 true 并添加 Authorization allowedHeader:
Plugins.Add(new CorsFeature(allowOriginWhitelist: new[] { "https://app.staging.mysite.com", "https://auth.staging.mysite.com" },
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowCredentials: true,
allowedHeaders: "Authorization, Content-Type"));
可以说,我们回到第一方,成功的身份验证,使用 ServiceStack 配置 CORS,但响应中仍然没有包含 RefreshToken 或 BearerToken:
经过更多的思考和reading this article,我不太确定这是一个 API 问题,但也许这是一个客户端 CORS 问题。我正在使用 ServiceStack Typescript 客户端,mythz 说这里包含凭据:
【问题讨论】:
标签: cors servicestack