当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core系列 48 Identity 身份模型自定义

asp.net core系列 48 Identity 身份模型自定义

2019年03月26日  | 移动技术网IT编程  | 我要评论

一.概述

  asp.net core identity提供了一个框架,用于管理和存储在 asp.net core 应用中的用户帐户。 identity添加到项目时单个用户帐户选择作为身份验证机制。 默认情况下,identity可以使用的 entity framework (ef) core 数据模型。 本文介绍如何自定义的身份标识模型。

 

  1.1 下面是已经存在的身份模型, 由以下实体类型组成:

实体类型

说明

关系

users 登录用户  

roles

角色

 

userclaims 用户拥有的权限 每个users有多个userclaims
usertokens 用户的身份验证令牌 每个users有多个usertokens
userlogins 将用户与登录相关联。 每个users有多个userlogins
roleclaims 角色拥有的权限 每个roles有多个roleclaims
userroles 用户和角色关联 每个users有多个roles

  

  1.2 默认模型的配置

    identity定义了许多从dbcontext继承以配置和使用模型的上下文类,此配置是使用上下文类的onmodelcreating方法中的ef core code first fluent api完成的。默认模型结构可以查看migration文件以及查看模型关系modelsnapshot文件,但要修改模型不在这里更改。下面是aspnetusers模型代码:

    下面是默认模型生成的数据表以及关系: 

  

二.模型自定义

   在ef上下文中当重写onmodelcreating方法时base.onmodelcreating方法首先调用; 接下来重写的会覆盖默认模型配置。

    public class applicationdbcontext : identitydbcontext<webappidentitydemouser>
    {
        public applicationdbcontext(dbcontextoptions<applicationdbcontext> options)
            : base(options)
        {
        }
        protected override void onmodelcreating(modelbuilder builder)
        {
            base.onmodelcreating(builder);
            // customize the asp.net core identity model and override the defaults if needed.
            // for example, you can rename the asp.net core identity table names and more.
            // add your customizations after calling base.onmodelcreating(builder);
        }
    }

  

   2.1 自定义用户数据

     在上篇有讲过自定义用户数据,这里在总结下。自定义用户数据支持通过继承identityuser类。 自定义类命名约定 {application}user。

     //定义{application}user扩展类,实现用户模型
      public class webappidentitydemouser : identityuser
     //使用{application}user作为上下文的泛型参数的类型:
      public class applicationdbcontext : identitydbcontext<webappidentitydemouser>
      //更新startup.configureservices以使用新{application}user类,最后生成迁移,同步数据库。
       services.adddefaultidentity<webappidentitydemouser>()
               .adddefaultui()
               .addentityframeworkstores<applicationdbcontext>();    

 

  2.2 更改主键类型  

     在创建数据库之后更改pk列的数据类型在许多数据库系统上都存在问题。更改pk通常涉及删除和重新创建表。因此,在创建数据库时,应在初始迁移中指定pk类型。下面是更改主键类型步骤:

     (1) 删除数据库,命令如下:

        drop-database  

    (2) 移除之前生成的迁移,命令如下:

        remove-migration

    (3) 修改user,role表主键类型,以及相关代码改动

    // 用户表设置主键为int
    public class webappidentitydemouser : identityuser<int>
    {
        /// <summary>
        /// full name
        /// </summary>
        [personaldata]
        public string name { get; set; }

        /// <summary>
        /// birth date
        /// </summary>
        [personaldata]
        public datetime dob { get; set; }
    }

   // 角色表设置主键为int
    public class webappidentitydemorole : identityrole<int>
    {

    }

     (4) 修改上下文

    public class applicationdbcontext : identitydbcontext<webappidentitydemouser, webappidentitydemorole,int>

    (5) 修改服务注册

       services.addidentity<webappidentitydemouser, webappidentitydemorole>()
       //如果使用identity scaffolder将identity文件添加到项目中,请删除对该项目的调用adddefaultui
       //.adddefaultui()
       .addentityframeworkstores<applicationdbcontext>()
       .adddefaulttokenproviders();

    (6) 生成迁移代码,命令如下

       add-migration identityschema

    (7) 同步数据库

      update-database identityschema

    此时表的主键类型已修改完成,包括关系表的外键类型也同步更新了,如下图所示:

 

   2.3 添加导航属性

     导航属性仅存在于ef模型中,而不存在于数据库中,如果导航关系没有改变,模型更改不需要更新数据库。如果更改关系的模型配置可能比进行其他更改更困难。必须注意取代现有的关系。下面示例是不改变模型关系,只是在user模型上添加导航属性以及在上下文中指定关系:

    public class webappidentitydemouser : identityuser<int>
    {
        /// <summary>
        /// full name
        /// </summary>
        [personaldata]
        public string name { get; set; }

        /// <summary>
        /// birth date
        /// </summary>
        [personaldata]
        public datetime dob { get; set; }

        //定义导航属性
        public virtual icollection<identityuserclaim<int>> claims { get; set; }
    }
     protected override void onmodelcreating(modelbuilder builder)
        {
            base.onmodelcreating(builder);
            // customize the asp.net core identity model and override the defaults if needed.
            // for example, you can rename the asp.net core identity table names and more.
            // add your customizations after calling base.onmodelcreating(builder);
            builder.entity<webappidentitydemouser>(b =>
            {
                // each user can have many userclaims
                b.hasmany(e => e.claims)
                    .withone()
                    .hasforeignkey(uc => uc.userid)
                    .isrequired();
            });
        }

    对于所有用户导航属性, 用户和角色导航属性,添加所有导航属性。参考官网文档。

 

  2.4  更改表/列名称,字段长度(上下文中更改)  

     protected override void onmodelcreating(modelbuilder builder)
        {
            base.onmodelcreating(builder);

            //更改表名称
            builder.entity<identityuser>(b =>
            {
                b.totable("myusers");
            });

            //更改表字段名称
            builder.entity<identityuserclaim<string>>(b =>
            {
                b.property(e => e.claimtype).hascolumnname("ctype");
                b.property(e => e.claimvalue).hascolumnname("cvalue");
            });

            //更改长度
            builder.entity<identityuser>(b =>
            {
                b.property(u => u.username).hasmaxlength(128);
            });    
        }

 

   参考文献

    identity

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网