使用Swiyu、Duende IdentityServer、ASP.NET Core Identity和.NET Aspire实现多因素身份验证
本文展示了如何在基于ASP.NET Core Identity和Duende IdentityServer构建的ASP.NET Core Web应用程序中,使用瑞士数字身份和信任基础设施作为多因素身份验证方法。该实现使用了Swiyu的通用容器来集成瑞士E-ID和可验证声明的OpenID标准。
项目代码:https://github.com/swiss-ssi-group/swiyu-idp-mfa-aspire-aspnetcore
本系列相关博客:
- Use swiyu, the Swiss E-ID to authenticate users with Duende and .NET Aspire
- Implement MFA using swiyu, the Swiss E-ID with Duende IdentityServer, ASP.NET Core Identity and .NET Aspire
- Implement forgot your password using swiyu, ASP.NET Core Identity and Aspire
设置
Web应用程序配置为使用OpenID Connect进行身份验证。它使用机密客户端,采用授权码流程和PKCE。OpenID Connect服务器使用Duende IdentityServer和ASP.NET Core Identity实现。为了进行E-ID验证,服务器集成了Swiyu通用容器以支持用于可验证声明的OpenID。Swiyu钱包用于管理E-ID。
集成ASP.NET Core Identity
在此设置中,引入了一个自定义的多因素身份验证提供商。SwiyuUserTwoFactorTokenProvider类负责集成Swiyu MFA。它实现了IUserTwoFactorTokenProvider接口,这是与ASP.NET Core Identity集成所必需的。此实现使用ApplicationUser类,允许应用程序支持额外的MFA步骤,而不仅仅是密码用户身份验证。
|
|
该接口在ASP.NET Core Identity中使用。MFA提供商通过AddTokenProvider方法添加。
|
|
设置数据库
MFA提供商需要数据持久化才能工作。SwiyuIdentity类用于此目的。
|
|
SwiyuIdentity类持久化从可验证凭证请求的数据。身份检查使用了以下声明:
- name
- family_name
- birth_place
- birth_date
该实体通过UserId与用户关联。
|
|
注册Swiyu作为MFA方法
设置完成后,经过身份验证的用户可以使用RegisterSwiyuModel Razor页面将Swiyu注册为MFA方法。这会创建一个可验证的凭证展示,用户可以从其控制的Swiyu钱包中出示凭证。使用JavaScript处理来自钱包的响应。
|
|
RegisterController Webhook
RegisterController用于处理来自Swiyu移动钱包的回调。该端点使用VerificationService来处理可验证凭证。如果响应正确,数据将持久化到数据库中。每个帐户只有一个凭证,实际实现时需要更加健壮。
|
|
禁用Swiyu MFA
可以使用DisableSwiyuModel Razor页面禁用Swiyu MFA。如果与其他MFA方法一起使用,需要改进对现有MFA的检查。
|
|
验证服务
VerificationService实现了创建可验证凭证展示和处理webhook响应的逻辑。该服务是根据Swiyu的文档实现的。
|
|
使用Swiyu登录
用户可以使用默认的登录Razor页面进行身份验证。LoginSwiyuMfaModel razor页面用于启动Swiyu验证。完成后,用户即被登录,身份验证完成。
|
|
登录
登录Razor页面会检查用户是否需要MFA,并重定向到正确的服务。
|
|
说明
此实现相当标准,用户可以使用Swiyu进行MFA。这不是一种防网络钓鱼的MFA,但提供了强大的身份验证。更好的方法是使用通行密钥进行身份验证,并使用Swyiu进行身份检查。
链接
- https://swiyu-admin-ch.github.io/
- https://www.eid.admin.ch/en/public-beta-e
- https://learn.microsoft.com/en-us/dotnet/aspire/get-started/aspire-overview
- https://www.npmjs.com/package/ngrok
- https://swiyu-admin-ch.github.io/specifications/interoperability-profile/
- https://andrewlock.net/converting-a-docker-compose-file-to-aspire/
- https://swiyu-admin-ch.github.io/cookbooks/onboarding-generic-verifier/
- https://github.com/orgs/swiyu-admin-ch/projects/2/views/2
标准
- https://identity.foundation/trustdidweb/
- https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html
- https://openid.net/specs/openid-4-verifiable-presentations-1_0.html
- https://datatracker.ietf.org/doc/draft-ietf-oauth-selective-disclosure-jwt/
- https://datatracker.ietf.org/doc/draft-ietf-oauth-sd-jwt-vc/
- https://datatracker.ietf.org/doc/draft-ietf-oauth-status-list/
- https://www.w3.org/TR/vc-data-model-2.0/