- 開発技術
ASP.NET Core Identityを簡単セットアップ
- ASP.NET Core
この記事を書いたチーム:tenko

ASP.NET Core Identityとは
【エンジニア募集中】フルリモート可◎、売上/従業員数9年連続UP、平均残業8時間、有給取得率90%、年休124日以上 etc. 詳細はこちらから>
ASP.NET Core Identityとは、.NET Coreアプリケーションに 認証(Authentication)と認可(Authorization) を簡単に追加するためのフレームワークです。
具体的には、ユーザー登録、ログイン、ログアウト機能、メールアドレスの検証、パスワードの検証機能、複数アプリケーション間のシングルサインオン(SSO)、ロールベースのアクセス制御、アカウントロックアウト機能などがあります。
Identityのセットアップ手順(SQL Server)
①パッケージインストール
|
1 2 |
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer |
②DbContextの作成
|
1 2 3 4 5 6 7 8 |
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } |
③Program.csでサービス登録
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddIdentity<IdentityUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); builder.Services.ConfigureApplicationCookie(options => { options.LoginPath = "/Account/Login"; }); app.UseAuthentication(); app.UseAuthorization(); |
④データベースマイグレーション
|
1 2 |
dotnet ef migrations add InitialIdentity dotnet ef database update |
【エンジニア募集中】フルリモートも◎(リモート率85.7%)、平均残業8時間、年休124日以上、有給取得率90% etc. 詳細はこちらから>



