https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=aspnetcore-2.1 参考地址

  • 标识模型包含七个实体类型:

    • User -表示的用户
    • Role -表示的角色
    • UserClaim -表示用户拥有的声明
    • UserToken -表示用户的身份验证令牌
    • UserLogin -将用户与一个登录名相关联
    • RoleClaim -表示将授予角色中的所有用户的声明
    • UserRole -加入将用户和角色相关联的实体

    实体类型关系

    这些实体类型通过以下方式彼此相关:

    • 每个User可以具有许多 UserClaims
    • 每个User可以具有许多 UserLogins
    • 每个User可以具有许多 UserTokens
    • 每个Role可以具有许多关联 RoleClaims
    • 每个User可以具有许多关联Roles,和每个Role可以与多个用户相关联
      • 联接表均由表示UserRole实体。
dotnet new mvc -o  IdentityMvc
cd IdentityMvc
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
Startup.cs->ConfigureServices

using IdentityMvc.Data;
using Microsoft.EntityFrameworkCore;
using IdentityMvc.Models;
using Microsoft.AspNetCore.Identity;

 1  services.AddDbContext<ApplicationDbContext>(options =>
 2                 options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
 3 
 4             services.AddIdentity<ApplicationUser, IdentityRole>()
 5                 .AddEntityFrameworkStores<ApplicationDbContext>()
 6                 .AddDefaultTokenProviders();
 7 
 8             services.Configure<IdentityOptions>(options =>
 9             {
10                 // Password settings
11                 options.Password.RequireDigit = true;
12                 options.Password.RequiredLength = 8;
13                 options.Password.RequireNonAlphanumeric = false;
14                 options.Password.RequireUppercase = true;
15                 options.Password.RequireLowercase = false;
16                 options.Password.RequiredUniqueChars = 6;
17 
18                 // Lockout settings
19                 options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
20                 options.Lockout.MaxFailedAccessAttempts = 10;
21                 options.Lockout.AllowedForNewUsers = true;
22 
23                 // User settings
24                 options.User.RequireUniqueEmail = true;
25             });
26 
27             services.ConfigureApplicationCookie(options =>
28             {
29                 // Cookie settings
30                 options.Cookie.HttpOnly = true;
31                 options.Cookie.Expiration = TimeSpan.FromDays(150);
32                 // If the LoginPath isn't set, ASP.NET Core defaults 
33                 // the path to /Account/Login.
34                 options.LoginPath = "/Account/Login";
35                 // If the AccessDeniedPath isn't set, ASP.NET Core defaults 
36                 // the path to /Account/AccessDenied.
37                 options.AccessDeniedPath = "/Account/AccessDenied";
38                 options.SlidingExpiration = true;
39             });
View Code

相关文章:

  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2021-10-02
  • 2021-06-22
  • 2022-02-27
  • 2018-11-21
  • 2021-10-16
猜你喜欢
  • 2021-12-25
  • 2022-12-23
  • 2021-05-15
  • 2021-07-10
  • 2022-12-23
相关资源
相似解决方案