文章出處

在今天,讀書有時是件“麻煩”事。它需要你付出時間,付出精力,還要付出一份心境。--僅以《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; }
    }

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()