使用 System.IdentityModel.Tokens.Jwt 从 1.1 迁移到 2.0 后,JWTAuthentication 在 asp.net core 2.0 中不起作用 - 5.1.
Posted
技术标签:
【中文标题】使用 System.IdentityModel.Tokens.Jwt 从 1.1 迁移到 2.0 后,JWTAuthentication 在 asp.net core 2.0 中不起作用 - 5.1.4 更新【英文标题】:JWTAuthentication not working in asp.net core 2.0 after migrate from 1.1 to 2.0 with System.IdentityModel.Tokens.Jwt - 5.1.4 update 【发布时间】:2018-02-18 03:07:21 【问题描述】:错误
错误 CS0619 'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' 已过时:'See https://go.microsoft.com/fwlink/?linkid=845470'
这是 JWT 认证的代码
app.UseJwtBearerAuthentication(new JwtBearerOptions
TokenValidationParameters = new TokenValidationParameters
IssuerSigningKey = key,
ValidAudience = tokenOptions.Audience,
ValidIssuer = tokenOptions.Issuer,
// When receiving a token, check that it is still valid.
ValidateLifetime = true,
// This defines the maximum allowable clock skew - i.e. provides a tolerance on the token expiry time
// when validating the lifetime. As we're creating the tokens locally and validating them on the same
// machines which should have synchronised time, this can be set to zero. Where external tokens are
// used, some leeway here could be useful.
ClockSkew = TimeSpan.FromMinutes(0)
);
相同的代码在 asp.net core 1.1 I 中运行良好,刚刚从 core 1.1 迁移到 core 2.0 并将 System.IdentityModel.Tokens.Jwt(5.1.1) 更新为 (5.1.4)
配置服务
RSAParameters keyParams = RsaKeyUtils.GetRandomKey();
key = new RsaSecurityKey(keyParams);
tokenOptions = new TokenAuthOptions()
Audience = TokenAudience,
Issuer = TokenIssuer,
SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
;
services.AddSingleton<TokenAuthOptions>(tokenOptions);
services.AddAuthorization(auth =>
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
);
我尝试了这个问题Authentication in dot net core preview-2.0 中发布的解决方案。但是解决方案无法正常工作,因为 IServiceCollection 不包含 AddJwtBearerAuthentication 的定义
尝试实现https://github.com/aspnet/Security/blob/c5b566ed4abffac4cd7011e0465e012cf503c871/samples/JwtBearerSample/Startup.cs#L47-L53 https://github.com/IdentityServer/IdentityServer4/issues/1055 https://blogs.msdn.microsoft.com/webdev/2017/04/06/jwt-validation-and-authorization-in-asp-net-core/
500 内部服务器错误。
相同的代码在 asp.net core 1.1 上运行良好。升级到 2.0 时会出现问题。
请任何人告诉我如何解决此问题。
【问题讨论】:
【参考方案1】:在 ASP.NET Core 2.0 中添加 Jwt Bearer Authentication 的方法发生了变化。请参阅您链接的示例的latest version。
您需要将 Jwt Bearer Authentication 进程注册为服务,而不是中间件组件。相关代码见ConfigureServices
:
services.AddAuthentication(...)
.AddJwtBearer(...);
在ConfigureServices
试试这个:
services.AddAuthentication()
.AddJwtBearer(jwt =>
jwt.TokenValidationParameters = new TokenValidationParameters
IssuerSigningKey = key,
ValidAudience = tokenOptions.Audience,
ValidIssuer = tokenOptions.Issuer,
// When receiving a token, check that it is still valid.
ValidateLifetime = true,
// This defines the maximum allowable clock skew - i.e. provides a tolerance on the token expiry time
// when validating the lifetime. As we're creating the tokens locally and validating them on the same
// machines which should have synchronised time, this can be set to zero. Where external tokens are
// used, some leeway here could be useful.
ClockSkew = TimeSpan.FromMinutes(0)
;
);
services.AddAuthorization(auth =>
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build());
);
并确保您在Configure
中有这个:
app.UseAuthentication();
由于我无法针对您的方案对此进行测试,因此很有可能第一次无法正常工作。当您包含来自此的任何进一步信息时,请务必具体说明。
【讨论】:
让我们在聊天中继续讨论:chat.***.com/rooms/154018/46129183。以上是关于使用 System.IdentityModel.Tokens.Jwt 从 1.1 迁移到 2.0 后,JWTAuthentication 在 asp.net core 2.0 中不起作用 - 5.1.的主要内容,如果未能解决你的问题,请参考以下文章
在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?
Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)