我想构建一个JWT Security Token Service(.net核心2+),以便在不使用IdentityServer或OAuth的情况下为许多API(.net核心2+)提供身份验证和授权,因为我想要无重定向。
"JWT Authenticator“运行良好,并且具有路由
然而,我正在努力提供步骤4和5。我在API1 1/startup.cs -> ConfigureServices尝试了许多选项,当调用get API1 1/ConfigureServices时,除了404之外,没有得到任何结果。ConfigureServices方法仍然是这样的:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = "http://localhost:6000"; // JWT Authenticator address
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = _configuration.GetValue<string>("JwtIssuer"),
ValidAudience = _configuration.GetValue<string>("JwtAudience"),
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(_configuration.GetValue<string>("JwtSecretKey"))),
ClockSkew = TimeSpan.Zero
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
是否可以使用类似AddJwtBearer的方法对步骤4和5进行配置?如果没有,我应该怎么做?使用API1的授权过滤器来拦截请求,并向JWT Authenticator发出请求以验证令牌/get声明/等?
发布于 2019-04-15 04:09:25
一些想法..。
您的4&5路径;通常您不会询问令牌的提供者它是否有效。您要做的是验证这一点,最有可能的是使用签名,这些参数可以-可选-从提供程序下载。
注意细微的区别:提供者本身并不检查它。
下一步:ClockSkew = TimeSpan.Zero
,这是非常小的。我至少会放它一马。
至少,拥有404
表示找不到资源。它不是401
或403
。此建议授权成功,但找不到请求的资源。
签名验证的典型实现:
注意,这并不能解决你的404。
options.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKeyResolver = (s, securityToken, identifier, parameters) =>
{
// get from provider
// todo: cache this
var json = new WebClient().DownloadString(
issuer + "/.well-known/jwks.json"); //typical endpoint
// serialize the result
var keys = JsonConvert.DeserializeObject<JsonWebKeySet>(json).Keys;
// cast the result to be the type expected by IssuerSigningKeyResolver
return (IEnumerable<SecurityKey>) keys;
}
//...
https://stackoverflow.com/questions/55679440
复制相似问题