2017-08-27 224 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); 
     } 
    }; 
    }); 
相關問題