-
Notifications
You must be signed in to change notification settings - Fork 0
MS_ASPNETCoreIdentity
- 戻る(ASP.NET Core、ASP.NET Identity)
- ASP.NET Core Identity
- ASP.NET Identity
- ASP.NET Identity の Community STS
- ASP.NET Identity(Identity 2.0)(net45)はメンテナンス・フェーズ
- 新規採用時は、ASP.NET Core Identity(Identity 3.0)(netcore)の使用を検討。
補足(最新化): 「Identity 3.0」という呼称は初期のもので、
現在は ASP.NET Core のバージョンに追従する
(ASP.NET Core 8 / 9 の Identity、という言い方をする)。
リポジトリもaspnet/Identityからdotnet/aspnetcoreに統合された。また、.NET 8 以降は次が加わっている。
追加 内容 Identity API エンドポイント MapIdentityApi<TUser>()で、SPA / モバイル向けの登録・ログイン API が 1 行で生えるパスキー対応 .NET 10 で Web Authentication API がサポートされた Blazor 用テンプレート 個別アカウント認証のスキャフォールドが Blazor に対応 ただし、Identity API エンドポイントは OAuth / OIDC の
代替ではない(Bearer トークンは独自形式で、外部 RP に配れない)。
STS が必要なら後述の Community STS を使う。
ASP.NET Identity(Identity 2.0)(net45)と、
OpenID / OAuth / OpenID Connectが分かっていれば差分の知識で済む。
- 既存の SMS を用いた 2 要素認証に加えて、
-
TOTPの二要素認証機能が追加されている。
- QR コード(
DNET_QRCode.md)の生成には、qrcode.js を使用する。 -
TOTPには、各種 Authenticator app を使用する。
- Google Authenticator app
- Microsoft Authenticator app
- QR コード(
補足(最新化): SMS による 2 要素認証は非推奨である
(NIST SP 800-63B は SMS を "restricted" と位置づけている)。
SIM スワップ・SS7 の傍受・端末の通知プレビューといった経路で
コードが第三者に渡りうる。強度は次の順である。
SMS < TOTP(Authenticator アプリ) < FIDO2 / パスキー新規実装は、TOTP を最低ラインとし、
FIDO2 / パスキー(Web Authentication API)を
目標にするのが望ましい。
- 参考
- ASP.NET Core Two Factor Authentication Using Google Authenticator - DZone Security
https://dzone.com/articles/aspnet-core-two-factor-authentication-using-google - How to set up two factor authentication in ASP.NET Core using Google Authenticator
https://medium.freecodecamp.org/how-to-set-up-two-factor-authentication-on-asp-net-core-using-google-authenticator-4b15d0698ec9 - ASP.NET Core での SMS で 2 要素認証
https://learn.microsoft.com/aspnet/core/security/authentication/2fa - ASP.NET Core で TOTP authenticator アプリの QR コード生成を有効にする
https://learn.microsoft.com/aspnet/core/security/authentication/identity-enable-qrcodes
- ASP.NET Core Two Factor Authentication Using Google Authenticator - DZone Security
-
OAuthAuthorizationServerMiddlewareは、
他の ASP.NET Identity の Community STS に
移行する必要がある。 -
「汎用認証サイト」では、STS をスクラッチ実装した。
| 版 | 方式 |
|---|---|
| 2.0 まで | 従来通り、MVC のビューが追加される。ただし Razor ではなく Tag ヘルパー(ASP.NET Core MVC)を使用する。 |
| 2.1 から | 従来の MVC のビューが追加されなくなっている。Razor Class Library により提供され、スキャフォールドで追加できる。 |
-
Razor
- とは言え、Razor でも OK。
- 汎用認証サイトでは、ASP.NET Identity の Razor 画面を移植できた。
補足: 2.1 以降の既定は
Microsoft.AspNetCore.Identity.UI(Razor Class Library)であり、
画面はアセンブリに埋め込まれていてプロジェクトには見えない。
カスタマイズしたい画面だけをdotnet aspnet-codegenerator identity -dc MyDbContext --files "Account.Login;Account.Register"のように部分的にスキャフォールドして上書きするのが定石である。
全部出すと、フレームワークの更新に追従できなくなる。
-
ASP.NET Identity(Identity 2.0)(net45)の
UsersAdmin、RolesAdmin的な。 - ASP.NET Core Identity(Identity 3.0)(netcore)に発見できず。
補足: 現在も管理画面は提供されていない(自作するしかない)。
UserManager<TUser>/RoleManager<TRole>の API を使って
自分で作る前提になっている。管理が本格的に必要なら、
Microsoft Entra ID や Keycloak のような
IdP 製品に寄せる判断も検討に値する。
- 2.0 と同じように、解除可能。
- 詳しくは後述を参照。
ASP.NET Identity(Identity 2.0)(net45)から、
ASP.NET Core Identity(Identity 3.0)(netcore)は、
マイグレーション(移行)というより再構築に近い。
-
移行情報もあるが、そもそも、
- ASP.NET と ASP.NET Core の互換性がそれほど高くない
(ASP.NET Coreへの移行)のだが、 - 以下の点も加えて、互換性は、あまり高くない。
- ASP.NET と ASP.NET Core の互換性がそれほど高くない
-
互換性の問題点
- ASP.NET Core Identity は、
.NET Standardレイヤではなく
.NET Coreレイヤ -
IUserインタフェースの廃止などがあり、
ApplicationUser、ApplicationRoleが継承するクラスが異なる。 - メソッド・シグネチャが異なる。
-
IUserStore、IRoleStore -
UserManager、RoleManager、SignInManager
-
- ASP.NET Core Identity は、
-
とは言え、汎用認証サイトでは
- ASP.NET Identity
- ASP.NET Core Identity
の両方をサポートしている(為せば成る的な)。
補足(データ移行で最も重要な点): パスワード ハッシュは移行できる。
PasswordHasher<TUser>は互換モードを持ち、
Identity 2.0 の形式(PasswordHasherCompatibilityMode.IdentityV2)を
検証できる。ユーザにパスワード再設定を強いる必要はない。一方、次は移行時に必ず引っかかる。
項目 注意 NormalizedUserName/NormalizedEmailCore で新設された列。 ToUpperInvariant()した値を投入しないとログインできないConcurrencyStamp新設。NULL だと更新時に例外になることがある SecurityStamp引き継がないと既存 Cookie が全部失効する(それが望ましい場合もある) Cookie 認証チケット 形式が非互換。移行時に全員再ログインになる 「移行後、誰もログインできない」の原因はほぼ
NormalizedUserNameである。
- 2 要素認証に新しい機能が追加されている。
- STS 機能(フレームワーク)が刷新されている。
-
ASP.NET Identity - CodePlex Archive
https://archive.codeplex.com/?p=aspnetidentityIdentity 2.0 is no longer under primary development.
No new features will be added, only bugs will be considered.
If Identity 3.0 does not have the feature you require then feel free to log a bug there. -
ASP.NET Core Identity(現在は
dotnet/aspnetcoreに統合)
https://github.com/dotnet/aspnetcore/tree/main/src/Identity
-
asp.net identity - Where are the Login and Register pages in an AspNet Core scaffolded app? - Stack Overflow
https://stackoverflow.com/questions/50802781/where-are-the-login-and-register-pages-in-an-aspnet-core-scafolded-app -
ASP.NET Core 2.1.0-preview1: Introducing Identity UI as a library | ASP.NET Blog
https://blogs.msdn.microsoft.com/webdev/2018/03/02/aspnetcore-2-1-identity-ui/ -
Scaffold Identity in ASP.NET Core projects | Microsoft Learn
https://learn.microsoft.com/aspnet/core/security/authentication/scaffold-identity
上級者は基本 EF(Entity Framework Core(MS_EntityFrameworkCore.md))キャンセル
(RDB ⇔ NoSQL、LDAP 切り替え等が必要な場合の考慮)。
- Writing an ASP.NET Core Identity Storage Provider from Scratch with RavenDB - Elemar JR
http://www.elemarjr.com/en/2017/05/writing-an-asp-net-core-identity-storage-provider-from-scratch-with-ravendb/
補足(どこまで実装するか): EF を外す場合、
必要なインタフェースだけを実装すればよい。全部は要らない。
インタフェース 必要になる場面 IUserStore<TUser>必須 IUserPasswordStore<TUser>ローカル パスワード認証をする IUserEmailStore<TUser>メール確認・パスワード リセット IUserRoleStore<TUser>ロールを使う IUserLoginStore<TUser>外部ログインを使う IUserSecurityStampStore<TUser>セッション失効を効かせる(実質必須) IUserTwoFactorStore<TUser>/IUserAuthenticatorKeyStore<TUser>2FA を使う IUserLockoutStore<TUser>ロックアウトを使う 実装していないインタフェースの機能を呼ぶと
NotSupportedExceptionになるため、
使う機能から逆算して選ぶのが正しい。
- ASP.NET Core Identity 用のカスタム ストレージ プロバイダー
https://learn.microsoft.com/aspnet/core/security/authentication/identity-custom-storage-providers
- Identity カテゴリーの記事一覧
http://mrgchr.hatenablog.com/archive/category/Identity- ASP.NET Core MVC にて、Entity Framework を使わずに ASP.NET Identity を利用する Part.0(準備編)
- 同 Part.1 / Part.2
- ASP.NET Core Identity をテンプレートからカスタマイズ
https://blog.nextscape.net/archives/Date/2017/12/aspnetidentity1 - ASP.NET Core Identity をゼロから構築する
https://blog.nextscape.net/archives/Date/2017/12/aspnetcoreidentity2
- ASP.NET Core での認証
https://learn.microsoft.com/aspnet/core/security/authentication/ - Facebook、Google、ASP.NET Core での外部プロバイダーの認証
https://learn.microsoft.com/aspnet/core/security/authentication/social/- Facebook / Twitter / Google / Microsoft アカウントの外部ログインのセットアップ
- 外部の OAuth 認証プロバイダー
- その他の要求と外部プロバイダーからのトークンを保存する
- Okta Authentication Quickstart Guides | Okta Developer
https://developer.okta.com/
- .NET Framework は、SAML のみらしい。
https://developers.onelogin.com/saml/c-and-aspnet - How to use OpenId Connect Authentication with Dotnet Core - OneLogin
https://www.onelogin.com/blog/how-to-use-openid-connect-authentication-with-dotnet-core
-
ASP.NET Identity から ASP.NET Core Identity へ移行してみた - しばやん雑記
https://blog.shibayan.jp/entry/20180710/1531150250NormalizedUserName、NormalizedEmailなどのNormalizedXXXXには、
.ToUpper()された値が入るもよう。これらの値は、DB の検索で利用。 -
ASP.NET Core への認証と ID を移行する
https://learn.microsoft.com/aspnet/core/migration/identity
- ASP.NET Core Security の概要
https://learn.microsoft.com/aspnet/core/security/
-
EF 前提
- ASP.NET Core Identity の概要
https://learn.microsoft.com/aspnet/core/security/authentication/identity - ASP.NET Core での ID モデルのカスタマイズ
https://learn.microsoft.com/aspnet/core/security/authentication/customize-identity-model
- ASP.NET Core Identity の概要
-
構成
- ASP.NET Core Identity を構成する
https://learn.microsoft.com/aspnet/core/security/authentication/identity-configuration - アカウントの確認とパスワードの回復
https://learn.microsoft.com/aspnet/core/security/authentication/accconfirm
- ASP.NET Core Identity を構成する
-
その他
- ASP.NET Core での Windows 認証を構成する
https://learn.microsoft.com/aspnet/core/security/authentication/windowsauth - ASP.NET Core Identity なしでの Cookie 認証を使用する
https://learn.microsoft.com/aspnet/core/security/authentication/cookie\ (ASP.NET Core における 認証を参照)
- ASP.NET Core での Windows 認証を構成する
-
ASP.NET Core での承認の概要
https://learn.microsoft.com/aspnet/core/security/authorization/introduction -
ASP.NET Core 2.0: Getting Started With Identity And Role Management - TechNet Wiki
https://social.technet.microsoft.com/wiki/contents/articles/51333.asp-net-core-2-0-getting-started-with-identity-and-role-management.aspx
- ASP.NET Core MVC で JWT を使った認証を実装する
https://tnakamura.hatenablog.com/entry/2017/08/04/jwt-bearer-authentication - ASP.NET Core 2.0 で JWT を使った認証を実装する
https://tnakamura.hatenablog.com/entry/2017/08/31/use-jwt-bearer-authentication-on-aspnetcore2
- ユーザー認証機能の実装 https://st40.xyz/one-run/article/333/
- UseInMemoryDatabase を使う際の注意点 https://st40.xyz/one-run/article/334/
- Authorize の Roles に複数のロールを設定する https://st40.xyz/one-run/article/526/
- パスワードのハッシュ化(PasswordHasher、Bcrypt) https://st40.xyz/one-run/article/527/
補足(パスワード ハッシュ): ASP.NET Core Identity の既定は
PBKDF2-HMAC-SHA256(IdentityV3形式)である。
.NET 8 で既定の反復回数が 10 万回に引き上げられた。Bcrypt / Argon2 に替えたい場合は
IPasswordHasher<TUser>を差し替える。
ただし、既存ハッシュの検証も通せるようにしておかないと
全ユーザがログインできなくなる(検証時に旧形式を判別し、
成功したら新形式で保存し直す「段階的な再ハッシュ」が定石)。
Tags: 移行, .NET開発, .NET Core, ASP.NET, ASP.NET MVC, ASP.NET Identity, 認証基盤, セキュリティ
このWikiは「Open棟梁Project」,「OSSコンソーシアム 開発基盤部会」によって運営されています。