【问题标题】:Add user First Name and Last Name to an ASP.NET Identity 2?将用户名字和姓氏添加到 ASP.NET Identity 2?
【发布时间】:2014-06-06 05:37:37
【问题描述】:

我改用新的 ASP.NET Identity 2。我实际上使用的是 Microsoft ASP.NET Identity Samples 2.0.0-beta2。

谁能告诉我在哪里以及如何修改代码,以便它存储用户的名字和姓氏以及用户详细信息。现在这会成为索赔的一部分吗?如果是,我该如何添加它?

我假设我需要在此处添加它,这是帐户控制器中的注册方法:

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                ViewBag.Link = callbackUrl;
                return View("DisplayEmail");
            }
            AddErrors(result);
        }

另外,如果我确实添加了名字和姓氏,那么这将存储在数据库中的什么位置?我需要在表格中为这些信息创建一个额外的列吗?

【问题讨论】:

    标签: c# asp.net-identity asp.net-identity-2


    【解决方案1】:

    您需要将其添加到您的 ApplicationUser 类中,因此如果您使用身份样本,我想您的 IdentityModels.cs 中有类似的内容

    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 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; }
    }
    

    然后当您注册用户时,您需要将它们添加到列表中,因为它们是在ApplicationUser 类中定义的

    var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = "Jack", LastName = "Daniels" };
    

    完成迁移后,名字和姓氏最终会出现在 AspNetUsers 表中

    【讨论】:

    • 你的方法是最新的吗?我四处搜索,发现了这个问题:stackoverflow.com/questions/21404935/… 另外,我还看到了一些其他添加内容作为声明的示例。你有没有研究过索赔的方式?
    • 是的,您所要求的与索赔无关...阅读this.. 没有理由使事情过于复杂
    • 你如何让这个作为声明出现在身份中?我是否需要其他流程将其添加到该用户的索赔表中?
    • @dima 我尝试在我自己的项目中遵循您的建议,但 FirstNameLastName 在迁移后并没有出现在我的数据库中,我在尝试注册时收到 500 Internal Server Error具有名字和姓氏的用户。 I made a post about it here。如果您对可能导致此问题的原因有任何想法,请告诉我。谢谢。 @Melina 或其他任何人,请随时加入。感谢您的宝贵时间。
    • -1。 @Melina 我相信声称是存储这些数据的正确方法。不对数据进行标准化可能看起来很奇怪(这就是为什么 dima 可能不喜欢这个想法),但看起来确实是为了这个目的而声明的。请参阅 stackoverflow.com/questions/21645323/…stackoverflow.com/questions/21362751/…。后者由 Microsoft ASP.NET Identity 团队的人员回答。
    【解决方案2】:

    我意识到这篇文章已经有几年的历史了,但是随着 ASP.NET Core 越来越受欢迎,我最终在这里遇到了类似的问题。接受的答案建议您更新用户数据模型以捕获此数据。我不认为这是一个糟糕的建议,但从我的研究来看,这是存储这些数据的正确方法。请参阅 What is the claims in ASP .NET IdentityUser.Identity.Name full name mvc5。后者由 Microsoft ASP.NET Identity 团队的人员回答。

    下面是一个简单的代码示例,展示了如何使用 ASP.NET Identity 添加这些声明:

    var claimsToAdd = new List<Claim>() {
        new Claim(ClaimTypes.GivenName, firstName),
        new Claim(ClaimTypes.Surname, lastName)
    };
    
    var addClaimsResult = await _userManager.AddClaimsAsync(user, claimsToAdd);
    

    【讨论】:

    • 这对我不起作用。我正在使用 ASP.Net.Core.2.2 :(
    猜你喜欢
    • 2018-03-17
    • 2013-06-13
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多