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

Models->ApplicationRole.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
public class ApplicationRole : IdentityRole
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }
} }

Models->ApplicationRoleClaim.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using IdentityMvc.Models; namespace IdentityMvc.Models
{ public class ApplicationRoleClaim : IdentityRoleClaim<string>
{
public virtual ApplicationRole Role { get; set; }
}
}

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 新建

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUserClaim : IdentityUserClaim<string>
{
public virtual ApplicationUser User { get; set; }
}
}

Models->ApplicationUserLogin.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUserLogin : IdentityUserLogin<string>
{
public virtual ApplicationUser User { get; set; }
}
}

Models->ApplicationUserRole.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUserRole : IdentityUserRole<string>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
}

Models->ApplicationUserToken.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUserToken : IdentityUserToken<string>
{
public virtual ApplicationUser User { get; set; }
}
}

Data->ApplicationDbContext.cs修改

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using IdentityMvc.Models;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Data
{
public class ApplicationDbContext
: IdentityDbContext<
ApplicationUser, ApplicationRole, string,
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin,
ApplicationRoleClaim, ApplicationUserToken>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Entity<ApplicationUser>(b =>
{
// Each User can have many UserClaims
b.HasMany(e => e.Claims)
.WithOne(e => e.User)
.HasForeignKey(uc => uc.UserId)
.IsRequired(); // Each User can have many UserLogins
b.HasMany(e => e.Logins)
.WithOne(e => e.User)
.HasForeignKey(ul => ul.UserId)
.IsRequired(); // Each User can have many UserTokens
b.HasMany(e => e.Tokens)
.WithOne(e => e.User)
.HasForeignKey(ut => ut.UserId)
.IsRequired(); // Each User can have many entries in the UserRole join table
b.HasMany(e => e.UserRoles)
.WithOne(e => e.User)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
b.ToTable("Sys_Users");
}); modelBuilder.Entity<ApplicationRole>(b =>
{
// Each Role can have many entries in the UserRole join table
b.HasMany(e => e.UserRoles)
.WithOne(e => e.Role)
.HasForeignKey(ur => ur.RoleId)
.IsRequired(); // Each Role can have many associated RoleClaims
b.HasMany(e => e.RoleClaims)
.WithOne(e => e.Role)
.HasForeignKey(rc => rc.RoleId)
.IsRequired();
b.ToTable("Sys_Roles");
});
modelBuilder.Entity<ApplicationUserClaim>(b =>
{
b.ToTable("Sys_UserClaims");
}); modelBuilder.Entity<ApplicationUserLogin>(b =>
{
b.ToTable("Sys_UserLogins");
}); modelBuilder.Entity<ApplicationUserToken>(b =>
{
b.ToTable("Sys_UserTokens");
}); modelBuilder.Entity<ApplicationRoleClaim>(b =>
{
b.ToTable("Sys_RoleClaims");
}); modelBuilder.Entity<ApplicationUserRole>(b =>
{
b.ToTable("Sys_UserRoles");
});
}
}
}

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

mvc core2.1 Identity.EntityFramework Core 实例配置 (四)的更多相关文章

  1. mvc core2.1 Identity.EntityFramework Core 配置 (一)

    https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=a ...

  2. webapi core2.1 Identity.EntityFramework Core进行配置和操作数据 (一)没什么用

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&am ...

  3. mvc core2.1 Identity.EntityFramework Core ROle和用户绑定查看 (八)完成

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  4. mvc core2.1 Identity.EntityFramework Core 用户Claims查看(七)

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  5. mvc core2.1 Identity.EntityFramework Core 注册 (二)

    Startup.cs-> Configure app.UseAuthentication(); //启动验证 Controllers->AccountController.cs 新建 us ...

  6. mvc core2.1 Identity.EntityFramework Core 导航状态栏(六)

    之前做的无法 登录退出,和状态,加入主页导航栏 Views ->Shared->_Layout.cshtml <div class="navbar-collapse col ...

  7. mvc core2.1 Identity.EntityFramework Core 用户列表预览 删除 修改 (五)

    用户列表预览 Controllers->AccountController.cs [HttpGet] public IActionResult Index() { return View(_us ...

  8. mvc core2.1 Identity.EntityFramework Core 登录 (三)

    Controllers->AccountController.cs 新建 [HttpGet] [AllowAnonymous] public async Task<IActionResul ...

  9. webapi core2.1 IdentityServer4.EntityFramework Core进行配置和操作数据

    https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践 vscode ...

随机推荐

  1. dell win10 productkey

  2. Java连接SqlServer 2008数据库

    将sqljdbc4.jar包添加到工程 连接SqlServer 2008数据库 import java.sql.Connection; import java.sql.DriverManager; i ...

  3. C语言转义字符'\'

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. Java多线程习题 ===重点 ,错题积累

    多线程重点,错题分析 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: . 12: 13: 14: 15:

  5. Myeclipse代码格式化

    Shift+Ctrl+F 别和搜狗输入法冲突,把搜狗输入法的设置取消掉. 当然,还可以设置,自动格式化: 步骤:

  6. HDU 2602 Bone Collectors(背包问题,模版)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. CodeMix入门基础知识

    CodeMix在线订购年终抄底促销!火爆开抢>> CodeMix入门 CodeMix是一个Eclipse插件,可以直接从Eclipse访问VS Code和为Code OSS构建的附加扩展的 ...

  8. mybatis column 和property

    mybatis map文件中 resultMap中column和sql查询结果对应, property和实体private对应 <resultMap id="VideoYcAppRes ...

  9. 使用kafka和zookeeper 构建分布式编译环境

    1:在每台机器上安装jdk, 脚本代码如下: 每一个机器上下载jdk,zookeeper,kafka 链接:https://www.oracle.com/technetwork/java/javase ...

  10. <算法><Union Find并查集>

    Intro 想象这样的应用场景:给定一些点,随着程序输入,不断地添加点之间的连通关系(边),整个图的连通关系也在变化.这时候我们如何维护整个图的连通性(即判断任意两个点之间的连通性)呢? 一个比较简单 ...