翻譯|使用教程|編輯:張蓉|2025-05-22 11:06:07.247|閱讀 1 次
概述:在當今的數字環境中,確保安全且用戶友好的身份驗證方法對于保護用戶數據和提升用戶體驗至關重要。通行密鑰認證(Passkey authentication)便是一種備受關注的方法,它利用 FIDO2 標準提供無密碼、抗網絡釣魚的安全保護。 在本文中,我們將探討如何使用 fido2-net-lib(一個簡化 FIDO2 身份驗證集成的.NET 庫)在ASP.NET Core 應用程序中實現通行密鑰認證。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Syncfusion 推出的ASP.NET Core UI 控件庫,由 Essential JS 2 驅動,是構建應用程序的得力工具。它擁有超 90 個組件,能滿足各類業務需求。該庫輕量且模塊化,按需引用可減少應用體積。組件性能卓越,響應式設計且觸摸友好,能適配多種設備。
在當今的數字環境中,確保安全且用戶友好的身份驗證方法對于保護用戶數據和提升用戶體驗至關重要。通行密鑰認證(Passkey authentication)便是一種備受關注的方法,它利用 FIDO2 標準提供無密碼、抗網絡釣魚的安全保護。
在本文中,我們將探討如何使用 fido2-net-lib(一個簡化 FIDO2 身份驗證集成的.NET 庫)在ASP.NET Core 應用程序中實現通行密鑰認證。
Essential Studio for ASP.NET Core 正版試用下載
通行密鑰認證是基于公鑰密碼學的無密碼身份驗證方式,用戶設備生成密鑰對(公鑰存服務器、私鑰留設備),認證時設備用私鑰簽名服務器發送的挑戰信息,無需傳輸身份數據即可完成驗證,兼具安全性與易用性。
要在我們的網站上實現通行密鑰認證,需先設置 fido2-net-lib 庫。請按照以下步驟操作:
步驟 1:安裝 fido2-net-lib
首先,在你的 .NET 項目中安裝 fido2-net-lib 包。你可以通過 NuGet 包管理器或 .NET CLI 進行安裝:
dotnet add package fido2-net-lib步驟 2:設置項目
using Fido2NetLib; public void ConfigureServices(IServiceCollection services) { services.AddFido2(options => { options.ServerDomain = "example.com", options.ServerName = "Example", options.Origins = "http://example.com" }; }步驟 4:通行密鑰注冊
請參考以下代碼示例生成認證選項。
· 用戶信息:第一個參數是 Fido2User 對象,包含用戶的顯示名稱、用戶名和唯一標識符等信息。
· 排除列表:第二個參數是 PublicKeyCredentialDescriptor 對象列表,可用于排除某些憑證的使用。例如,可防止用戶多次注冊同一設備。
· 認證器選擇:第三個參數是 AuthenticatorSelection 對象,用于定義選擇認證器的標準。例如,可指定僅允許跨平臺認證器。
· 認證傳輸偏好:最后一個參數是 AttestationConveyancePreference 枚舉,指示認證數據應如何傳輸給依賴方,選項包括 None(無)、Indirect(間接)和 Direct(直接)。
生成的認證選項將以 JSON 響應形式發送到用戶設備,用戶設備將使用這些選項創建新的密鑰對并返回一個認證對象。
注冊憑證
要完成注冊流程,需實現客戶端邏輯,使用認證選項創建新憑證。這包括通過 navigator.credentials.create () API 調用傳入認證選項。
當客戶端返回響應時,我們驗證并存儲憑證。
[Route("/register")] public async Task<JsonResult> MakeCredential([FromBody] AuthenticatorAttestationRawResponse attestationResponse, CancellationToken cancellationToken) { // 1. Get the options that we sent to the client var jsonOptions = HttpContext.Session.GetString("fido2.attestationOptions"); var options = CredentialCreateOptions.FromJson(jsonOptions); // 2. Create a callback so that lib can verify that the credential id is unique to this user IsCredentialIdUniqueToUserAsyncDelegate callback = static async (args, cancellationToken) => { var users = await Storage.GetUsersByCredentialIdAsync(args.CredentialId, cancellationToken); if (users.Count > 0) return false; return true; }; // 2. Verify and make the credentials var credential = await fido.MakeNewCredentialAsync(attestationResponse, options, callback, cancellationToken: cancellationToken); // 3. Store the credentials in the database Storage.AddCredential(options.User, new Credential { Id = credential.Id, PublicKey = credential.PublicKey, SignCount = credential.SignCount, RegDate = DateTimeOffset.UtcNow, Transports = credential.Transports, IsBackedUp = credential.IsBackedUp, userId = userId, DeviceName = "User Device 1" }); // 4. Return "ok" to the client return Json(credential.Status);}
MakeNewCredentialAsync 方法用于驗證客戶端的認證響應,并在驗證成功時創建新憑證。該方法確保注冊流程的安全性和完整性。
用戶完成設備注冊后,需實現認證流程以驗證其身份。該過程包括生成斷言選項、將其發送至用戶設備,然后驗證響應以完成用戶登錄。
生成斷言選項
請參考以下代碼示例生成斷言選項。
[Route("/AssertionOptions")] public JsonResult AssertionOptionsPost(string email) { // 1. Get user from DB var user = DemoStorage.GetUser(username); // 2. Get registered credentials from the database List<PublicKeyCredentialDescriptor> existingCredentials = Storage.GetCredentialsByUser(user).Select(c => c.Descriptor).ToList(); // 3. Assertion options var options = fido.GetAssertionOptions( existingCredentials, UserVerificationRequirement.Preferred ); // 4. Temporarily store options, session/in-memory cache/redis/db HttpContext.Session.SetString("fido2.assertionOptions", options.ToJson()); // 5. Return options to the client return Json(options); }
GetAssertionOptions 方法生成設備認證所需的斷言選項(含網站、用戶信息及挑戰值)。
驗證響應流程:
· 格式轉換:將斷言選項從 Base64URL 轉為 Uint8Array(適配 WebAuthn 加密操作)。
· 生成斷言:調用navigator.credentials.get()傳入選項,觸發設備生成含加密證明的斷言。
· 響應準備:將斷言組件轉為 Base64URL 格式以便網絡傳輸。
· 服務器驗證:將響應發送至服務器完成身份驗證。
當客戶端返回響應時,驗證憑證以使用憑證登錄。
當客戶端返回響應時,驗證憑證以使用憑證登錄。
[Route("/VerifyPasskey")] public async Task<JsonResult> MakeAssertion(VerifyPasskeyResponse clientResponse, CancellationToken cancellationToken) { // 1. Get the assertion options we sent the client and remove them from storage var jsonOptions = HttpContext.Session.GetString("fido2.assertionOptions"); HttpContext.Session.Remove("fido2.assertionOptions"); var options = AssertionOptions.FromJson(jsonOptions); // 2. Get registered credentials from the database StoredCredential creds = Storage.GetCredentialById(clientResponse.Id); // 3. Get credential counter from the database var storedCounter = creds.SignatureCounter; // 4. Create a callback to check if userhandle owns the credentialId IsUserHandleOwnerOfCredentialIdAsync callback = async (args) => { List<StoredCredential> storedCreds = await DemoStorage.GetCredentialsByUserHandleAsync(args.UserHandle); return storedCreds.Exists(c => c.Descriptor.Id.SequenceEqual(args.CredentialId)); }; // 5. Make the assertion var res = await fido.MakeAssertionAsync(clientResponse, options, creds.PublicKey, storedCounter, callback); // 6. Store the updated counter DemoStorage.UpdateCounter(res.CredentialId, res.Counter); // 7. return OK to client return Json(res); }
· 反序列化:首先將接收到的斷言響應轉換為可處理的數據格式。
· 憑證檢索:從數據庫中獲取用戶存儲的憑證信息(如與通行密鑰關聯的公鑰)。
· 構建驗證請求:創建包含挑戰值、用戶憑證及其他驗證參數的請求對象。
· 驗證簽名:通過 FIDO2 庫驗證斷言的簽名有效性,確認用戶對憑證的所有權。
若驗證成功,用戶將被授予系統訪問權限。該方法是實現安全無密碼認證的核心組件,能夠簡化認證流程,同時提升安全性與用戶體驗。
慧都科技是?家?業數字化解決?案公司,專注于軟件、?油與?業領域,以深?的業務理解和?業經驗,幫助企業實現智能化轉型與持續競爭優勢。
慧都科技作為 Syncfusion 的中國區合作伙伴,Syncfusion 作為 UI 組件研發領域的領先技術提供商,提供 Essential Studio 等強大工具,助力企業實現高效的應用開發與管理。Essential Studio 包括 1900 多個組件和框架,支持 WinForms 等多個主流開發平臺,其組件功能強大,可滿足大量數據處理需求。Essential Studio 提供豐富的學習資源,包括視頻教程、文檔和知識庫,幫助開發者快速掌握使用方法。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn