加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Dotnet核心2.0使用身份与JwtBearer身份验证

发布时间:2020-12-16 07:20:45 所属栏目:asp.Net 来源:网络整理
导读:在我的Asp.Net核心web api中,我使用Identity与Jwt承载认证.它没有任何大惊小怪,工作顺利.这是代码, ConfigureServices(): services.AddIdentityApplicationUser,IdentityRoleint() .AddEntityFrameworkStoresDataContext,int() .AddDefaultTokenProviders()
在我的Asp.Net核心web api中,我使用Identity与Jwt承载认证.它没有任何大惊小怪,工作顺利.这是代码,

ConfigureServices():

services.AddIdentity<ApplicationUser,IdentityRole<int>>()
            .AddEntityFrameworkStores<DataContext,int>()
            .AddDefaultTokenProviders();

配置():

app.UseJwtBearerAuthentication(new JwtBearerOptions()
            {
                AutomaticAuthenticate = true,AutomaticChallenge = true,TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer = "localhost:4200",ValidAudience = "localhost:4200",ValidateIssuerSigningKey = true,IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SuperSecretKey_GetThisFromAppSettings")),ValidateLifetime = true
                }
            });

今天我升级到.net core 2.0和整个技术堆栈.从有限的帮助中我可以修改这样的代码..

ConfigureServices()

services.AddIdentity<ApplicationUser,ApplicationRole>()
                .AddEntityFrameworkStores<DataContext>()
                .AddDefaultTokenProviders();   



services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Authority = "localhost:4200";
                    options.Audience = "localhost:4200";
                    options.RequireHttpsMetadata = false;
                    options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuerSigningKey = true,ValidateIssuer = true,ValidateLifetime = true,ValidIssuer = "localhost:4200",IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SuperSecretKey_GetThisFromAppSettings"))
                };
            });

配置()

app.UseAuthentication();

现在身份验证无效.看起来它在内部配置为使用Cookie身份验证.

还有其他人遇到过这种情况吗?对此有任何帮助非常感谢!

谢谢,

解决方法

如果我从MS站点正确理解

https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

Identity添加cookie并将默认身份验证设置为cookie方案.
尝试改变你的

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

services.AddAuthentication(o => {
  o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读