zoukankan      html  css  js  c++  java
  • mvc core2.1 Identity.EntityFramework Core 实例配置 (四)

      https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=aspnetcore-2.1 实践

    Models->ApplicationRole.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel.DataAnnotations;
     4 using System.Linq;
     5 using System.Threading.Tasks;
     6 using Microsoft.AspNetCore.Identity;
     7 
     8 namespace IdentityMvc.Models
     9 {
    10     public class ApplicationRole : IdentityRole
    11     {
    12         public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
    13         public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }
    14     }
    15 
    16 }
    View Code

    Models->ApplicationRoleClaim.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel.DataAnnotations;
     4 using System.Linq;
     5 using System.Threading.Tasks;
     6 using Microsoft.AspNetCore.Identity;
     7 using IdentityMvc.Models;
     8 
     9 namespace IdentityMvc.Models
    10 {
    11 
    12     public class ApplicationRoleClaim : IdentityRoleClaim<string>
    13     {
    14         public virtual ApplicationRole Role { get; set; }
    15     }
    16 }
    View Code

    Models-> ApplicationUser 添加

            public string Note {get;set;} //自定义添加字段
    
            public virtual ICollection<ApplicationUserClaim> Claims { get; set; }
            public virtual ICollection<ApplicationUserLogin> Logins { get; set; }
            public virtual ICollection<ApplicationUserToken> Tokens { get; set; }
            public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }

    Models-> ApplicationUserClaim.cs 新建

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Identity;
     6 
     7 namespace IdentityMvc.Models
     8 {
     9     // Add profile data for application users by adding properties to the ApplicationUser class
    10     public class ApplicationUserClaim : IdentityUserClaim<string>
    11     {
    12         public virtual ApplicationUser User { get; set; }
    13     }
    14 }
    View Code

    Models->ApplicationUserLogin.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Identity;
     6 
     7 namespace IdentityMvc.Models
     8 {
     9     // Add profile data for application users by adding properties to the ApplicationUser class
    10     public class ApplicationUserLogin : IdentityUserLogin<string>
    11     {
    12         public virtual ApplicationUser User { get; set; }
    13     }
    14 }
    View Code

    Models->ApplicationUserRole.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Identity;
     6 
     7 namespace IdentityMvc.Models
     8 {
     9     // Add profile data for application users by adding properties to the ApplicationUser class
    10     public class ApplicationUserRole : IdentityUserRole<string>
    11     {
    12         public virtual ApplicationUser User { get; set; }
    13         public virtual ApplicationRole Role { get; set; }
    14     }
    15 }
    View Code

    Models->ApplicationUserToken.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Identity;
     6 
     7 namespace IdentityMvc.Models
     8 {
     9     // Add profile data for application users by adding properties to the ApplicationUser class
    10     public class ApplicationUserToken : IdentityUserToken<string>
    11     {
    12         public virtual ApplicationUser User { get; set; }
    13     }
    14 }
    View Code

    Data->ApplicationDbContext.cs修改

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
     6 using Microsoft.EntityFrameworkCore;
     7 using IdentityMvc.Models;
     8 using Microsoft.AspNetCore.Identity;
     9 
    10 namespace IdentityMvc.Data
    11 {
    12     public class ApplicationDbContext
    13     : IdentityDbContext<
    14         ApplicationUser, ApplicationRole, string,
    15         ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin,
    16         ApplicationRoleClaim, ApplicationUserToken>
    17 {
    18     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    19         : base(options)
    20     {
    21     }
    22     
    23     protected override void OnModelCreating(ModelBuilder modelBuilder)
    24     {
    25         base.OnModelCreating(modelBuilder);
    26 
    27             modelBuilder.Entity<ApplicationUser>(b =>
    28             {
    29                 // Each User can have many UserClaims
    30                 b.HasMany(e => e.Claims)
    31                     .WithOne(e => e.User)
    32                     .HasForeignKey(uc => uc.UserId)
    33                     .IsRequired();
    34 
    35                 // Each User can have many UserLogins
    36                 b.HasMany(e => e.Logins)
    37                     .WithOne(e => e.User)
    38                     .HasForeignKey(ul => ul.UserId)
    39                     .IsRequired();
    40 
    41                 // Each User can have many UserTokens
    42                 b.HasMany(e => e.Tokens)
    43                     .WithOne(e => e.User)
    44                     .HasForeignKey(ut => ut.UserId)
    45                     .IsRequired();
    46 
    47                 // Each User can have many entries in the UserRole join table
    48                 b.HasMany(e => e.UserRoles)
    49                     .WithOne(e => e.User)
    50                     .HasForeignKey(ur => ur.UserId)
    51                     .IsRequired();
    52                 b.ToTable("Sys_Users");
    53             });
    54 
    55             modelBuilder.Entity<ApplicationRole>(b =>
    56             {
    57                 // Each Role can have many entries in the UserRole join table
    58                 b.HasMany(e => e.UserRoles)
    59                     .WithOne(e => e.Role)
    60                     .HasForeignKey(ur => ur.RoleId)
    61                     .IsRequired();
    62 
    63                 // Each Role can have many associated RoleClaims
    64                 b.HasMany(e => e.RoleClaims)
    65                     .WithOne(e => e.Role)
    66                     .HasForeignKey(rc => rc.RoleId)
    67                     .IsRequired();
    68                 b.ToTable("Sys_Roles");
    69             });
    70             modelBuilder.Entity<ApplicationUserClaim>(b =>
    71             {
    72                 b.ToTable("Sys_UserClaims");
    73             });
    74 
    75             modelBuilder.Entity<ApplicationUserLogin>(b =>
    76             {
    77                 b.ToTable("Sys_UserLogins");
    78             });
    79 
    80             modelBuilder.Entity<ApplicationUserToken>(b =>
    81             {
    82                 b.ToTable("Sys_UserTokens");
    83             });
    84 
    85             modelBuilder.Entity<ApplicationRoleClaim>(b =>
    86             {
    87                 b.ToTable("Sys_RoleClaims");
    88             });
    89 
    90             modelBuilder.Entity<ApplicationUserRole>(b =>
    91             {
    92                 b.ToTable("Sys_UserRoles");
    93             });
    94         }
    95     }
    96 }
    View Code

    包含关系建立,增加字段,修改自动生成在表名字3个功能,更详细的设置如长度,修改字段名字,可以通过连接参考

  • 相关阅读:
    ASCII、GBK、Unicode、UTF-8、ISO-8859-1等常见字符编码介绍
    HTTP协议简介
    关于无知的一点思考
    Java 8 新特性之lambda表达式
    Java 8 新特性之新的日期时间库
    【java】<Jsoup>获取网页中的图片
    【数据结构】二叉树
    【转载】Android中UI线程与后台线程交互设计的5种方法
    【数据结构】广义表
    【c语言】数据结构(约瑟夫生者死者游戏的问题)
  • 原文地址:https://www.cnblogs.com/LiuFengH/p/9419250.html
Copyright © 2011-2022 走看看