MDP.Security.Claims 6.1.18.2-beta.1

This is a prerelease version of MDP.Security.Claims.
There is a newer version of this package available.
See the version list below for details.
dotnet add package MDP.Security.Claims --version 6.1.18.2-beta.1
NuGet\Install-Package MDP.Security.Claims -Version 6.1.18.2-beta.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MDP.Security.Claims" Version="6.1.18.2-beta.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MDP.Security.Claims --version 6.1.18.2-beta.1
#r "nuget: MDP.Security.Claims, 6.1.18.2-beta.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MDP.Security.Claims as a Cake Addin
#addin nuget:?package=MDP.Security.Claims&version=6.1.18.2-beta.1&prerelease

// Install MDP.Security.Claims as a Cake Tool
#tool nuget:?package=MDP.Security.Claims&version=6.1.18.2-beta.1&prerelease

MDP.AspNetCore.Authentication

MDP.AspNetCore.Authentication是開源的.NET開發套件,協助開發人員快速建立整合ASP.NET Core身分驗證的應用系統。提供Line、Google、Facebook等OAuth身分驗證模組、及Jwt等Token身分驗證模組,用以簡化開發流程並滿足多變的商業需求。

快速開始

模組功能

MDP.AspNetCore.Authentication-模組功能.png

模組掛載

MDP.AspNetCore.Authentication擴充ASP.NET Core既有的身分驗證,加入Line、Google、Facebook等OAuth身分驗證模組、及Jwt等Token身分驗證模組的掛載功能。開發人員可以透過Config設定,掛載在執行階段使用的身分認證。

// Config設定 - Line身分驗證模組
{
  "Authentication": {
    "Line": {
      "ClientId": "Xxxxx",
      "ClientSecret": "Xxxxx"
    }
  }
}
- 命名空間:Authentication
- 掛載的身分驗證模組:Line
- Line身分驗證模組的客戶編號:ClientId="Xxxxx"。(Xxxxx填入Channel ID)
- Line身分驗證模組的客戶密碼:ClientSecret="Xxxxx"。(Xxxxx填入Channel Secret)
// Config設定 - Jwt身分驗證模組
  "Authentication": {
    "Jwt": {
      "Credentials": [
        {
          "Scheme": "JwtBearer",
          "Header": "Authorization",
          "Prefix": "Bearer ",
          "Algorithm": "HS256",
          "SignKey": "12345678901234567890123456789012",
          "Issuer": "MDP",
          "ExpireMinutes": 30
        }
      ]
    }
  },
- 命名空間:Authentication
- 掛載的身分驗證模組:Jwt
- 憑證清單:Credentials
- 憑證名稱:Scheme="JwtBearer"。
- 憑證標頭:Header="Authorization"。(從HTTP Request的哪個Header取得Token,常見:Authorization、x-api-token)
- 憑證前綴:Prefix="Bearer"。(Token的前綴字,常見:"Bearer"、"")
- 簽章算法:Algorithm="HS256"。(Token所使用的簽章演算法,支持:HSxxx、RSxxx)
- 簽章密鑰:SignKey="12345..."。(Token所使用的簽章密鑰,支持:PEM格式密鑰)
- 憑證發行:Issuer="MDP"。(檢核用,Token的核發單位)

Remote身分驗證

MDP.AspNetCore.Authentication-Remote身分驗證.png

MDP.AspNetCore.Authentication擴充ASP.NET Core既有的身分驗證,加入Remote身分驗證流程。用來確認通過OAuth身分驗證的身分資料,是否為已知用戶、是否需要引導註冊、是否拒絕存取,並於最終完成登入。

  • MDP.AspNetCore.Authentication加入Controller的擴充方法LoginAsync,用來發起Remote身分驗證流程。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static async Task<ActionResult> LoginAsync(this Controller controller, string scheme, string returnUrl = null)
- controller:執行的Controller物件。
- scheme:OAuth身分驗證的名稱。
- returnUrl:完成Remote身分驗證之後,要跳轉的功能頁面路徑。
- Task<ActionResult>:回傳值,流程跳轉頁面。
  • 開發人員使用LoginAsync發起Remote身分驗證流程之後,系統就會依照輸入的scheme名稱進行OAuth身分驗證。完成之後,系統會將取得的身分資料拿來執行Remote身分登入,將身分資料寫入Cookie提供後續流程使用。(在這個階段還沒完成登入,開發人員在這個階段可以使用Controller的擴充方法RemoteAuthenticateAsync,來取得目前Remote身分登入的身分資料)
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static Task<ClaimsIdentity> RemoteAuthenticateAsync(this Controller controller)
- controller:執行的Controller物件。
- Task<ClaimsIdentity>:回傳值,目前Remote身分登入的身分資料。
  • 完成Remote身分登入之後,系統會檢查是否有覆寫AuthenticationProvider的實作存在。有的話,會使用該實作覆寫的RemoteExchange方法,將Remote身分登入的身分資料轉換為本地的身分資料。轉換過程,可以依照專案需求比對會員資料庫、比對AD...用來確認身分資料。能確認身分資料的就回傳本地的身分資料進行Local身分登入,不能確認身分資料的則是回傳null進行後續流程。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationProvider

// 類別方法:
public virtual ClaimsIdentity RemoteExchange(ClaimsIdentity remoteIdentity)
- remoteIdentity:Remote身分登入的身分資料。
- Task<ClaimsIdentity>:回傳值,經過比對之後回傳本地的身分資料。
  • Remote身分登入的身分資料轉換為本地的身分資料的過程中,發現不能確認身分資料的時候。系統會參考Config設定,沒有設定RegisterPath的系統則是會跳轉至拒絕存取頁面;有設定RegisterPath的系統會跳轉至該註冊頁面,並於註冊完畢將取得的身分資料拿來執行Local身分登入。
// Config設定
{
  "Authentication": {
    "RegisterPath": "/Account/Register"
  }
}
- 命名空間:Authentication
- 註冊頁面路徑:RegisterPath="/Account/Register"。(null是預設值,代表不須跳轉至註冊頁面)
  • 當系統將取得的身分資料拿來執行Local身分登入,會將身分資料寫入Cookie提供後續流程使用。(在這個階段已經完成登入,開發人員在這個階段可以使用Controller的擴充方法RemoteAuthenticateAsync,來取得目前Remote身分登入的身分資料)
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static Task<ClaimsIdentity> LocalAuthenticateAsync(this Controller controller)
- controller:執行的Controller物件。
- Task<ClaimsIdentity>:回傳值,目前Local身分登入的身分資料。

Remote身分綁定

MDP.AspNetCore.Authentication-Remote身分綁定.png

完成登入之後,開發人員可以使用MDP.AspNetCore.Authentication提供的Remote身分綁定流程,用來綁定用戶所擁有的其他OAuth身分驗證。

  • MDP.AspNetCore.Authentication加入Controller的擴充方法LinkAsync,用來發起Remote身分綁定流程。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static async Task<ActionResult> LinkAsync(this Controller controller, string scheme, string returnUrl = null)
- controller:執行的Controller物件。
- scheme:OAuth身分驗證的名稱。
- returnUrl:完成Remote身分綁定之後,要跳轉的功能頁面路徑。
- Task<ActionResult>:回傳值,流程跳轉頁面。
  • 開發人員使用LinkAsync發起Remote身分綁定流程之後,系統就會依照輸入的scheme名稱進行OAuth身分驗證。完成之後,系統會將取得的身分資料拿來執行Remote身分登入,將身分資料寫入Cookie提供後續流程使用。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static Task<ClaimsIdentity> RemoteAuthenticateAsync(this Controller controller)
- controller:執行的Controller物件。
- Task<ClaimsIdentity>:回傳值,目前Remote身分登入的身分資料。
  • 完成Remote身分登入之後,系統會檢查是否有覆寫AuthenticationProvider的實作存在。有的話,會使用該實作覆寫的RemoteLink方法,將Remote身分登入的身分資料與本地的身分資料進行綁定。綁定過程,可以依照專案需求將綁定資料寫入會員資料庫、寫入AD欄位...用來提供下次Remote身分驗證時使用。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationProvider

// 類別方法:
public virtual void RemoteLink(ClaimsIdentity remoteIdentity, ClaimsIdentity localIdentity)
- remoteIdentity:Remote身分登入的身分資料。
- localIdentity:Local身分登入的身分資料。

Local身分驗證

MDP.AspNetCore.Authentication-Local身分驗證.png

MDP.AspNetCore.Authentication擴充ASP.NET Core既有的身分驗證,加入Local身分驗證流程。用來讓開發人員透過資料庫帳號密碼驗證、或是AD帳號密碼認證之後,直接建立身分資料來執行Local身分登入,將身分資料寫入Cookie提供後續流程使用。

  • MDP.AspNetCore.Authentication加入Controller的擴充方法LoginAsync,用來發起Local身分驗證流程。
// 命名空間:
MDP.AspNetCore.Authentication

// 類別定義:
public class AuthenticationControllerExtensions

// 擴充方法
public static async Task<ActionResult> LoginAsync(this Controller controller, ClaimsIdentity localIdentity, string returnUrl = null)
- controller:執行的Controller物件。
- localIdentity:Local身分登入的身分資料。
- returnUrl:完成Remote身分驗證之後,要跳轉的功能頁面路徑。
- Task<ActionResult>:回傳值,流程跳轉頁面。

Token身分驗證

MDP.AspNetCore.Authentication-Token身分驗證.png

MDP.AspNetCore.Authentication擴充ASP.NET Core既有的身分驗證,加入Token身分驗證流程。用來將通過Token身分驗證的身分資料,提供給後續流程使用。

  • 開發人員可以在HTTP Request封包裡加入代表身分資料的Token,用來發起Token身分驗證流程。
// HTTP headers - JwtBearer
Authorization:Bearer Xxxxxxxxxxxxxxxx

// HTTP headers - ApiToken
X-Api-Token:Xxxxxxxxxxxxxxxx

模組使用

加入專案

MDP.AspNetCore.Authentication預設獨立在MDP.Net專案範本外,依照下列操作步驟,即可建立加入MDP.AspNetCore.Authentication的專案。

  • 在命令提示字元輸入下列指令,使用MDP.Net專案範本建立專案。
// 建立API服務、Web站台
dotnet new install MDP.WebApp
dotnet new MDP.WebApp -n WebApplication1
  • 使用Visual Studio開啟專案。在專案裡使用NuGet套件管理員,新增下列NuGet套件。
MDP.AspNetCore.Authentication

設定參數

建立包含MDP.AspNetCore.Authentication模組的專案之後,在專案裡可以透過Config設定,掛載在執行階段使用的身分驗證及相關參數。

// Config設定 - Line身分驗證模組
{
  "Authentication": {
    "Line": {
      "ClientId": "Xxxxx",
      "ClientSecret": "Xxxxx"
    }
  }
}
- 命名空間:Authentication
- 掛載的身分驗證模組:Line
- Line身分驗證模組的客戶編號:ClientId="Xxxxx"。(Xxxxx填入Channel ID)
- Line身分驗證模組的客戶密碼:ClientSecret="Xxxxx"。(Xxxxx填入Channel Secret)
// Config設定
{
  "Authentication": {
    "RegisterPath": "/Account/Register"
  }
}
- 命名空間:Authentication
- 註冊頁面路徑:RegisterPath="/Account/Register"。(null是預設值,代表不須跳轉至註冊頁面)

註冊AuthenticationProvider

建立包含MDP.AspNetCore.Authentication模組的專案之後,在專案裡可以註冊AuthenticationProvider實作,來覆寫RemoteExchange、RemoteLink。

using MDP.AspNetCore.Authentication;

namespace MDP.Members
{
    [MDP.Registration.Service<AuthenticationProvider>(singleton: true)]
    public class MemberAuthenticationProvider : AuthenticationProvider
    {
        // Methods
        public override ClaimsIdentity RemoteExchange(ClaimsIdentity remoteIdentity)
        {
            // ...
        }

        public override void RemoteLink(ClaimsIdentity remoteIdentity, ClaimsIdentity localIdentity)
        {
            // ...
        }
    }
}
"MDP.Members": {
  "MemberAuthenticationProvider": {}
}

版本更新

MDP.AspNetCore.Authentication 6.1.8.1

  • 重構AuthenticationProvider,讓他更容易被理解。

MDP.AspNetCore.Authentication 6.1.8

  • 加入Microsoft身分驗證。

  • 加入AzureAD身分驗證。

  • 重構MDP.AspNetCore.Authentication,簡化登入邏輯與流程。

MDP.AspNetCore.Authentication 6.1.5

  • 跟隨 MDP.Net進版。
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on MDP.Security.Claims:

Package Downloads
MDP.AspNetCore.Authentication

MDP.Net Library

MDP.Security.Tokens.Jwt

MDP.Net Library

MDP.AspNetCore.Authorization

MDP.Net Library

MDP.BlazorCore

MDP.Net Library

MDP.AspNetCore.Authentication.OAuthSSO.Server

MDP.Net Library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.7.4 584 6/4/2024
8.0.7 545 5/21/2024
8.0.6 192 5/14/2024
8.0.5 356 5/2/2024
8.0.3 73 5/1/2024
8.0.2 133 4/26/2024
8.0.2-beta03 77 4/23/2024
8.0.2-beta02 70 4/23/2024
8.0.2-beta01 72 4/22/2024
8.0.1 207 4/6/2024
6.1.20 219 4/5/2024
6.1.18.2-beta.1 60 10/2/2023
6.1.18 101 4/4/2024
6.1.17 100 4/4/2024
6.1.15 221 3/30/2024
6.1.14 189 3/30/2024
6.1.13 323 1/30/2024
6.1.11 331 12/16/2023
6.1.10 311 12/12/2023
6.1.8 424 9/13/2023