zoukankan      html  css  js  c++  java
  • Owin+ASP.NET Identity浅析系列(二)扩展用户属性

    在今天,读书有时是件“麻烦”事。它需要你付出时间,付出精力,还要付出一份心境。--仅以《Owin+ASP.NET Identity浅析系列》来祭奠那逝去的……

      上一篇博客讲了用户登录注册问题,这篇说下如何扩展用户属性,毕竟我们的项目中用户不可能只有用户名、邮箱、手机号不是,下面为我们的用户表新增两个属性:所在城市和年龄

    第一步:修改数据库表,新增两个字段

    create table aspnetusers
    (
    	Id char(32) primary key,
    	Email varchar(50) null comment '用户邮箱',
    	EmailConfirmed bit not null comment '是否认证邮箱',
    	PasswordHash varchar(100) null comment '账户密码',
    	SecurityStamp varchar(100) null comment '防伪印章',
    	PhoneNumber varchar(100) null comment '用户手机',
    	PhoneNumberConfirmed bit not null comment '是否认证手机',
    	TwoFactorEnabled bit not null comment '是否启用双重身份验证',
    	LockoutEndDateUtc datetime null comment '锁定结束时间',
    	LockoutEnabled bit not null comment '是否启用锁定',
    	AccessFailedCount int not null comment '登陆失败次数',
    	UserName varchar(50) not null comment '用户名称',
    	City varchar(50) null comment '所在城市',
    	Age int default 0 not null comment '今年几何'
    ) comment '用户表';
    

    第二步:在ApplicationUser类中加入对应的属性

    // 可以通过向 ApplicationUser 类添加更多属性来为用户添加配置文件数据。若要了解详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=317594。
    public class ApplicationUser : IdentityUser
    {
        public virtual string City { get; set; }
    
        public virtual int Age { get; set; }
    
        public ApplicationUser()
        {
            this.Id = System.Guid.NewGuid().ToString("N");
        }
    
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // 请注意,authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // 在此处添加自定义用户声明
            return userIdentity;
        }
    }
    

     第三步:修改用户注册代码(一套是默认的注册方式,一套是自定义注册方式)

      默认注册方式代码修改如下:

    var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, City = model.City, Age = model.Age };
    
    var result = await UserManager.CreateAsync(user, model.Password);
    
    if (result.Succeeded)
    {
        return Json(new { Flag = true, Content = "注册成功!!!" }, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(new { Flag = false, Content = "注册失败!!!" }, JsonRequestBehavior.AllowGet);
    }
    

       自定义注册方式代码修改如下:

    var db = new Data.DataContext();
    
    db.Members.Add(new Data.DomainModels.Member()
    {
        Id = Guid.NewGuid().ToString("N"),
        SecurityStamp = Guid.NewGuid().ToString(),
        Email = model.Email,
        PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password),
        UserName = model.UserName,
        City=model.City,
        Age=model.Age
    });
    
    var result = await db.SaveChangesAsync();
    
    if (result > 0)
    {
        return Json(new { Flag = true, Content = "注册成功!!!" }, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(new { Flag = false, Content = "注册失败!!!" }, JsonRequestBehavior.AllowGet);
    }
    

     Data.DomainModels.Member是映射数据库user表的实体类

        [Table("aspnetusers")]
        public class Member
        {
            public string Id { get; set; }
    
            public string Email { get; set; }
    
            public bool EmailConfirmed { get; set; }
    
            public string PasswordHash { get; set; }
    
            public string SecurityStamp { get; set; }
    
            public string PhoneNumber { get; set; }
    
            public bool PhoneNumberConfirmed { get; set; }
    
            public bool TwoFactorEnabled { get; set; }
    
            public DateTime? LockoutEndDateUtc { get; set; }
    
            public bool LockoutEnabled { get; set; }
    
            public int AccessFailedCount { get; set; }
    
            public string UserName { get; set; }
    
            public string City { get; set; }
    
            public int Age { get; set; }
        }
    
  • 相关阅读:
    聚合根、实体、值对象
    哀悼的CSS 把网站变成灰色
    Ubuntu13.04更换aptget源
    JS判断用户终端,跳转到不同的页面.
    分享一个使用的FireFox 截图插件小巧方便
    Linux 下面FireFox 看CCTV直播
    Ubuntu 11.10后 Guest账户禁用!
    修改Ubuntu的启动画面plymouth
    ubuntu开机自启动小键盘
    Linux 用cat做图片种子||Windows 用copy做图片种子
  • 原文地址:https://www.cnblogs.com/amywechat/p/6038301.html
Copyright © 2011-2022 走看看