天天看點

NETCore3.1 使用Jwt保護api

摘要

本文示範如何向有效使用者提供jwt,以及如何在webapi中使用該token通過JwtBearerMiddleware中間件對使用者進行身份認證。

認證和授權差別?

首先我們要弄清楚認證(Authentication)和授權(Authorization)的差別,以免混淆了。認證是确認的過程中你是誰,而授權圍繞是你被允許做什麼,即權限。顯然,在确認允許使用者做什麼之前,你需要知道他們是誰,是以,在需要授權時,還必須以某種方式對使用者進行身份驗證。

什麼是JWT?

根據維基百科的定義,JSON WEB Token(JWT),是一種基于JSON的、用于在網絡上聲明某種主張的令牌(token)。JWT通常由三部分組成:頭資訊(header),消息體(payload)和簽名(signature)。

頭資訊指定了該JWT使用的簽名算法:

```c#

header = '{"alg":"HS256","typ":"JWT"}'

`HS256`表示使用了HMAC-SHA256來生成簽名。

消息體包含了JWT的意圖:

```c#
payload = '{"loggedInAs":"admin","iat":1422779638}'//iat表示令牌生成的時間           

未簽名的令牌由

base64url

編碼的頭資訊和消息體拼接而成(使用"."分隔),簽名則通過私有的key計算而成:

key = 'secretkey'

unsignedToken = encodeBase64(header) + '.' + encodeBase64(payload)

signature = HMAC-SHA256(key, unsignedToken)

最後在未簽名的令牌尾部拼接上`base64url`編碼的簽名(同樣使用"."分隔)就是JWT了:

```c#
token = encodeBase64(header) + '.' + encodeBase64(payload) + '.' + encodeBase64(signature)

# token看起來像這樣: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI           

JWT常常被用作保護服務端的資源(resource),用戶端通常将JWT通過HTTP的

Authorization

header發送給服務端,服務端使用自己儲存的key計算、驗證簽名以判斷該JWT是否可信:

Authorization: Bearer eyJhbGci...<snip>...yu5CSpyHI

### 準備工作

使用vs2019建立webapi項目,并且安裝nuget包

```c#
Microsoft.AspNetCore.Authentication.JwtBearer           

Startup類

ConfigureServices 添加認證服務

services.AddAuthentication(options =>

{

options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

}).AddJwtBearer(options =>

options.SaveToken = true;

options.RequireHttpsMetadata = false;

options.TokenValidationParameters = new TokenValidationParameters()

ValidateIssuer = true,

ValidateAudience = true,

ValidAudience = "https://xx",

ValidIssuer = "https://xx",

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecureKeySecureKeySecureKeySecureKeySecureKeySecureKey"))

};

});

Configure 配置認證中間件

`app.UseAuthentication();//認證中間件`

### 建立一個token

添加一個登入model命名為LoginInput

```c#
public class LoginInput
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }           

添加一個認證控制器命名為AuthenticateController

[Route("api/[controller]")]

public class AuthenticateController : Controller

[HttpPost]

[Route("login")]

public IActionResult Login([FromBody]LoginInput input)

//從資料庫驗證使用者名,密碼

//驗證通過 否則 傳回Unauthorized

//建立claim
        var authClaims = new[] {
            new Claim(JwtRegisteredClaimNames.Sub,input.Username),
            new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString())
        };
        IdentityModelEventSource.ShowPII = true;
        //簽名秘鑰 可以放到json檔案中
        var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecureKeySecureKeySecureKeySecureKeySecureKeySecureKey"));

        var token = new JwtSecurityToken(
               issuer: "https://xx",
               audience: "https://xx",
               expires: DateTime.Now.AddHours(2),
               claims: authClaims,
               signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
               );

        //傳回token和過期時間
        return Ok(new
        {
            token = new JwtSecurityTokenHandler().WriteToken(token),
            expiration = token.ValidTo
        });
    }
}           
### 添加api資源

**利用預設的控制器****WeatherForecastController**

- - 添加個Authorize标簽
  - 路由調整為:[Route("api/[controller]")] 代碼如下

```c#
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class WeatherForecastController : ControllerBase           

運作項目

  1. 發現傳回時401未認證,下面擷取token
  2. 通過使用者和密碼擷取token
  3. 如果我們的憑證正确,将會傳回一個token和過期日期,然後利用該令牌進行通路
  4. 利用token進行請求
  5. 現請求狀态200!