2017-07-06 141 views

回答

2

JSON網絡令牌(JWT)是OAUTH的實現2.

ASP.NET Core and Angular 2 by Valerio De Sanctis解釋瞭如何在ASP.NET中實現核心JWT提供非常詳細。下面是示例代碼 -

public void Configure(IApplicationBuilder app...) 
{ 
    ... 
    // Add a custom Jwt Provider to generate Tokens 
    app.UseJwtProvider(); 

    // Add the Jwt Bearer Header Authentication to validate Tokens 
    app.UseJwtBearerAuthentication(new JwtBearerOptions() 
    { 
     AutomaticAuthenticate = true, 
     AutomaticChallenge = true, 
     RequireHttpsMetadata = false, 
     TokenValidationParameters = new TokenValidationParameters() 
     { 
      IssuerSigningKey = JwtProvider.SecurityKey, 
      ValidateIssuerSigningKey = true, 
      ValidIssuer = JwtProvider.Issuer, 
      ValidateIssuer = false, 
      ValidateAudience = false 
     } 
    }); 
    ... 
} 

不幸的是,示例代碼是寫在ASP.NET核心的前一版本相比使用project.json。我將該項目轉換爲csproj,地址爲GitHub