【问题标题】:Dotnet core 2.0 Use Identity with JwtBearer AuthenticationDotnet core 2.0 使用带有 JwtBearer 身份验证的身份
【发布时间】:2018-02-04 21:27:58
【问题描述】:

在我的 Asp.Net 核心 web api 中,我使用了带有 Jwt 不记名身份验证的 Identity。它工作顺利,没有任何大惊小怪。这是代码,

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 身份验证。

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

谢谢,

【问题讨论】:

    标签: asp.net asp.net-web-api asp.net-identity jwt .net-core-2.0


    【解决方案1】:

    如果我从 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;
    })
    

    【讨论】:

    • 嘿,成功了!非常感谢。但我还有一个问题。您知道如何在未经授权的访问期间停止默认重定向到登录页面吗?我需要将 http 状态代码发送给客户端,而不是重定向。
    • 没问题,我认为有更好的方法,但是因为您使用 Identity 和 CookieAuthentication 作为第二个身份验证方案,您可以使用登录路径,因为它会退回到它。 services.ConfigureApplicationCookie(options => { options.LoginPath = "/somecontr/somefunc"; });并返回您需要的任何东西。 public IActionResult somefunc() return StatusCode(418)
    【解决方案2】:

    回答问题:

    您知道如何在未经授权的访问期间停止默认重定向到登录页面吗?

    我发现这篇由 PioneerCode 撰写的关于 dotnet core 1 的博文可能会有所帮助。

    这就是我实现它的方式并且它起作用了:

    services.ConfigureApplicationCookie(options => { options.LoginPath = "/api/login";
        options.Events = new CookieAuthenticationEvents
        {
          OnRedirectToLogin = ctx =>
          {
            if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200)
            {
              ctx.Response.StatusCode = 401;
              return Task.FromResult<object>(null);
            }
    
            ctx.Response.Redirect(ctx.RedirectUri);
            return Task.FromResult<object>(null);
          }
        };
      });
    

    【讨论】:

    • 这是cookie,不是承载
    猜你喜欢
    • 2018-01-28
    • 2018-05-28
    • 2018-04-05
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多