ASP.NETCore6MVC:SignInManager不能与AddAuthentication一起使用来处理JwtBearer令牌。
创始人
2024-09-17 14:31:08
0次

在ASP.NET Core 6 MVC应用程序中使用JwtBearer身份验证时,需要将AddJwtBearer和AddAuthentication同时添加到Startup.cs文件中的ConfigureServices方法中。但是,当我们在SignInManager中使用AddAuthentication时,出现了问题,SignInManager似乎无法识别JwtBearer令牌。

要解决这个问题,我们需要手动以编程方式将JwtBearer令牌与Identity一起使用。为此,我们需要更改ConfigureServices中的代码,以便在添加AddAuthentication之前添加Identity。然后,我们可以使用AddJwtBearer方法来将JwtBearer令牌添加到应用程序中,并使用AddIdentity方法将Identity添加到应用程序中。最后,在AddAuthentication后,我们可以添加UseAuthentication以启用认证。

以下是示例代码:

public void ConfigureServices(IServiceCollection services) { services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders();

services.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = Configuration["Jwt:Issuer"],
        ValidAudience = Configuration["Jwt:Issuer"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
    };
});

services.AddAuthentication(options =>
{
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = Configuration["Jwt:Issuer"],
        ValidAudience = Configuration["Jwt:Issuer"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
    };
});

services.AddControllers

相关内容

热门资讯

大家学习交流!wepokerp... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepokerplus到底是挂了吗解...
专业讨论!!wepoker脚本... 很多人使用云端 AI 时,都担心wepoker脚本下载、内部资料上传带来隐私风险。这款本地运行的 A...
玩家必备科技!!wepoker... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker插件辅助解析、 AI ...
终于知道!wpk辅助插件,we... 终于知道!wpk辅助插件,wepoker怎么开挂助手(原来是真的有透视)AI 智能体黑科技再上新,自...
玩家必看科普!wpk辅助器是真... 您好:wpk辅助器是真的吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
程序员教你!wepoker破解... 程序员教你!wepoker破解器有用吗,微扑克有挂吗神器(竟然真的是有开挂)AI 是一款可以让一直输...
可靠技巧!wepoker新号好... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
可靠技巧!德扑之星透视辅助插件... 您好:德扑之星透视辅助插件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
记者揭秘!!wepoker辅助... wepoker辅助工具AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数...
发现玩家!wpk辅助插件,德普... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...