c# - Asp.net Core Identity - 操作步骤
程序员文章站
2022-03-09 10:44:54
...
1. Define a new class, derived from IdentityUser
public class AppUser : IdentityUser
{
public string DisplayName { get; set; }
}
2. In your DataContext, change
'public class DataContext : DbContext'
to
'public class DataContext : IdentityDbContext<AppUser>'
3. In your DataContext, method: 'protected override void OnModelCreating(ModelBuilder builder)' add code:
base.OnModelCreating(builder);
4. Execute cmd:
dotnet ef migrations add "AddIdentity" -p Persistence -s API
5. In start project: "API" directory, execute cmd:
dotnet ef database update
7. In start project: "API" directory, add nuget package: 'Microsoft.AspNetCore.Identity.UI'
8. In start project: "API", method: 'public void ConfigureServices(IServiceCollection services)' add code below
// Add Identity Support
var builder = services.AddIdentityCore<AppUser>();
var identityBuilder = new IdentityBuilder(builder.UserType, builder.Services);
identityBuilder.AddEntityFrameworkStores<DataContext>();
identityBuilder.AddSignInManager<SignInManager<AppUser>>();
9. Add some seed data, using 'UserManager<AppUser> userManager':
if (!userManager.Users.Any())
{
var users = new List<AppUser>
{
new AppUser { DisplayName = "Bob", UserName = "bob", Email = "[email protected]" },
new AppUser { DisplayName = "Tom", UserName = "tom", Email = "[email protected]" },
new AppUser { DisplayName = "Jane", UserName = "jane", Email = "[email protected]" }
};
foreach(var user in users)
{
await userManager.CreateAsync(user, "Pa$$w0rd");
}
}
推荐阅读
-
asp.net core identity 配置cookie
-
ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—启用用户管理
-
asp.net core identity学习1
-
ASP.NET Core Identity 实战(4)授权过程
-
ASP.NET Core Identity 实战(3)认证过程
-
ASP.NET CORE[练习]-Identity-Assertion
-
Asp.net core Identity的配置
-
c#,asp.net,asp.net MVC,asp.net Core 获取客户端IP
-
asp.net c# 对GridView编辑,删除,更新,取消的操作
-
用例子看ASP.NET Core Identity是什么?