从ASP.NET Core 1迁移到ASP.NET Core 2后,令牌身份验证停止工作

编程入门 行业动态 更新时间:2024-10-27 12:40:57
本文介绍了从ASP.NET Core 1迁移到ASP.NET Core 2后,令牌身份验证停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我关注了这个站点将我的网站从ASP.NET Core 1迁移到ASP.NET Core 2。

I followed this site to migrate my website from ASP.NET Core 1 to ASP.NET Core 2.

但是在更改了ASP Identity之后,我的身份验证停止了工作。

However after doing the ASP Identity changes my authentication stopped working.

我的服务如下:

public void ConfigureServices(IServiceCollection services) { services.AddSingleton(_ => Configuration); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddResponseCaching(); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); var secretKey = Configuration.GetSection("AppSettings")["SecretKey"]; var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)); var tokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = signingKey, ValidateIssuer = true, ValidIssuer = "arnvanhoutte", ValidateAudience = true, ValidAudience = "User", ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters; }); services.AddWebSocketManager(); services.AddMvc(); services.AddTransient<Seed>(); }

我有 app.UseAuthentication(); 在我的Configure方法的底部。

And I have app.UseAuthentication(); at the bottom of my Configure method.

但是当我检查 var isAuthenticated = User.Identity.IsAuthenticated; 在控制器中它总是说为false。

However when I check var isAuthenticated = User.Identity.IsAuthenticated; this in a controller it always says false. It worked before though so I don't understand why this stopped working.

推荐答案

我在迁移过程中遇到了类似的问题

I faced a similar issue during the migration

对我来说,登录有效,没有任何问题,但是后续请求失败,重定向到登录页面,而不是抛出401(这导致404作为其a网站) api我没有任何登录页面!)。

For me the login worked without any issue,but subsequent request failed with a redirect to the login page instead of throwing a 401(which caused a 404 as its a web api i didn't had any login page!).

将defaultauthenticationscheme和DefaultChallengeScheme添加到addauthentication中对我来说很成功。

按如下所示更改addauthentication以使jwt身份验证有效!

Changing the addauthentication as follows to make the jwt authentication work!

services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters });

更多推荐

从ASP.NET Core 1迁移到ASP.NET Core 2后,令牌身份验证停止工作

本文发布于:2023-11-16 22:22:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   身份验证   工作   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!