加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

使用Asp.Net Identity 2在AspNetUserClaims中存储用户信息有什么

发布时间:2020-12-15 22:22:21 所属栏目:asp.Net 来源:网络整理
导读:我想存储一些额外的用户信息.从我的了解以下是通常的选择: public class ApplicationUser : IdentityUser { public async TaskClaimsIdentity GenerateUserIdentityAsync(UserManagerApplicationUser manager) { // Note the authenticationType must match
我想存储一些额外的用户信息.从我的了解以下是通常的选择:
public class ApplicationUser : IdentityUser {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }

这里添加了FirstName和LastName,它们将在AspNetUsers表中显示为其他字段.

然而,现在使用Asp.Net身份认证还可以在AspNetUserClaims表中存储这种类型的信息.

有人可以向我解释这是AspNetUserClaims中可以存储的那种信息.如果是这样,那么任何人都有这样的例子.

解决方法

在一天结束时,您的登录用户将被转换成存储在ClaimIdentity中的一系列声明,这些声明以HttpContext.User.Identity表示您的用户.您可以选择将FirstName / LastName作为列存储在用户表中,然后您可以明确地读出并转换为相应的声明(如果需要),或者您可以将它们直接存储在AspnetUserClaims表中的声明(仅存储它们作为两个字符串列),默认情况下将自动添加到您的用户的声明身份.这两种方法或多或少相当,但最终取决于个人偏好.

BTW是您在所有用户的ClaimIdentity中希望使用这些的唯一原因,如果您要保存db命令以显示该名称,并且始终使用ClaimIdentity中的FirstName / LastName声明.如果您提取用户,并使用user.FirstName,则在生成名称声明时也没有太多价值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读