gpt4 book ai didi

c# - 使用 System.IdentityModel.Tokens.Jwt 从 1.1 迁移到 2.0 后,JWTAuthentication 在 asp.net core 2.0 中不起作用 - 5.1.4 更新

转载 作者:太空狗 更新时间:2023-10-30 01:14:06 26 4
gpt4 key购买 nike

错误

错误 CS0619 'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' 已过时:'参见 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 时出现问题。

请任何人告诉我如何解决这个问题。

最佳答案

在 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();

由于我无法针对您的场景对此进行测试,因此很有可能它不会在第一时间起作用。当您包含由此产生的任何进一步信息时,请务必具体说明。

关于c# - 使用 System.IdentityModel.Tokens.Jwt 从 1.1 迁移到 2.0 后,JWTAuthentication 在 asp.net core 2.0 中不起作用 - 5.1.4 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46129183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com