2017-08-27 78 views
3

在我的Asp.Net核心网络api我使用身份与Jwt承载身份验证。它工作顺利,没有任何大惊小怪。以下是一个代码,Dotnet核心2.0使用身份与JwtBearer身份验证

ConfigureServices())

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

配置(:我升级到.NET 2.0的核心

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 
       } 
      }); 

,今日,整个技术叠加。从可用在那里有限的帮助,我已经修改了代码,这样的..

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

是否有其他人遇到过这种情况?任何对此的帮助真的很感激!

感谢,

回答

5

如果我理解正确,从MS网站

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

身份增加了饼干和设置默认身份验证cookie的方案。 试着改变你的

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

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

嘿,成功了!并且非常感谢。但我还有一个问题。你知道如何在未经授权的访问期间停止默认重定向到登录页面吗?我不需要重定向,而需要将http状态码发送给客户端。 – Wijitha

+0

没问题,我假设有更好的方法,但因为您使用Identity和CookieAuthentication作为第二个身份验证方案,所以您可以使用登录路径,因为它会回退到它。 services.ConfigureApplicationCookie(options => { options.LoginPath =“/ somecontr/somefunc”; });并返回你需要的任何东西。 public IActionResult somefunc()返回StatusCode(418) –

1

在回答这个问题:

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

我发现这篇博文的PioneerCode为dotnet核心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); 
     } 
    }; 
    }); 
相关问题