响应中未返回承载令牌或刷新令牌,怀疑CORS-ServiceStack
在我的开发环境中,一切正常。然而,在登台阶段,现在我们肯定有不同的域和CORS问题,我已经完全解决了,除了潜在的一个问题 关于API的CORS配置,我正在使用响应中未返回承载令牌或刷新令牌,怀疑CORS-ServiceStack,cors,servicestack,Cors,servicestack,在我的开发环境中,一切正常。然而,在登台阶段,现在我们肯定有不同的域和CORS问题,我已经完全解决了,除了潜在的一个问题 关于API的CORS配置,我正在使用Microsoft.AspNetCore.CORSNuget软件包,因为我无法找到使用ServiceStack CORS功能将某些域列入白名单的方法,并且我阅读了ServiceStack文档。。。我现在知道,当我实例化ServiceStack功能时,有一个重载构造函数: CorsFeature(ICollection<string&g
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);
下面是一个实际响应屏幕截图:
就像这个ServiceStack。在这个引用的链接中,我看到@mythz这样说,“提供者应该是”凭证“。这让我想知道我是否有CORS问题,因为我使用的是Microsoft.AspNetCore.CORS
而不是ServiceStack CORS功能,Access Control Allow Credentials
为false,ServiceStack代码正在检查此值,并且不会返回bear令牌。这是我使用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));
}
}
最后一个布尔参数将allowCredentials
设置为true
这将导致HTTP OK响应,这意味着凭据良好:
但我们有飞行前请求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 configure CORS成功进行身份验证,但响应中仍然没有包括RefreshToken或BealerToken:
经过更多的思考,实际上,我不太确定这是一个API问题,但也许这是一个客户端CORS问题。我正在使用ServiceStack Typescript客户端,mythz在这里说,凭据包括:
您的问题缺少AuthFeature注册,但要使JWT令牌能够在Auth响应中返回,您需要包括
JwtAuthProvider
以及您希望支持的任何其他AuthProvider
默认情况下,JWT配置为仅通过安全(即https://)连接返回JWT令牌,如果您在开发期间未通过https://查看此令牌,或者您正在通过SSL终止代理,则需要允许JWT通过以下非安全连接:
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new JwtAuthProvider(AppSettings){ RequireSecureConnection = false },
}
));
否则,应在使用ServiceStack内置AuthProviders时启用的“身份验证请求”上返回JWT令牌,如果您使用的是自定义AuthProvider,则可能需要通过以下设置指示请求执行身份验证:
authService.Request.Items[Keywords.DidAuthenticate] = true;
刷新令牌
因为您需要使用Auth存储库,或者您的Auth提供者需要实现IUserSessionSource
将经过身份验证的会话转换为JWT
创建JWT令牌的另一种方法是调用
ConvertSessionToken
服务,或者Ajax客户端可以通过调用它。感谢@mythz的另一个快速回复。你的回答很有道理尽管我对其中一部分感到惊讶,在开发localhost和HTTP开发环境期间,刷新令牌和承载令牌总是在响应中返回……根据您的回答,我认为如果这一切都发生了,我将永远不会在我的开发环境中体验到承载令牌在响应中。我在开发环境中的经验是不是因为我的auth api一开始没有注册AuthFeature?@BrianOgden如果AuthFeature
wan没有注册,你就根本无法进行身份验证,但是如果未注册JwtAuthProvider
,则不会创建任何JWT令牌。您知道@mythz我可能已将本地和开发环境的RequiresCureConnection设置为false,我前面没有代码,但我想一定是这样。对于我的登台环境,我确实有一个SSL终止的NGINX代理,尽管我不知道我是如何设置我的第一个代理的,直到你这样说
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new JwtAuthProvider(AppSettings){ RequireSecureConnection = false },
}
));
authService.Request.Items[Keywords.DidAuthenticate] = true;