将用户名字和姓氏添加到 ASP.NET Identity 2? [英] Add user First Name and Last Name to an ASP.NET Identity 2?

查看:22
本文介绍了将用户名字和姓氏添加到 ASP.NET Identity 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I changed over to use the new ASP.NET Identity 2. I'm actually using the Microsoft ASP.NET Identity Samples 2.0.0-beta2.

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

Can anyone tell me where and how I can modify the code so that it stores a user First and Last name along with the user details. Would this now be part of a claim and if so how could I add it ?

我想我需要在这里添加这个,这是帐户控制器中的注册方法:

I assume I would need to add this here which is the register method in the account controller:

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

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

Also if I did add the first and last names then where is this stored in the database? Do I need to create an additional column in a table for this information?

推荐答案

您需要将它添加到您的 ApplicationUser 类中,因此如果您使用 Identity Samples,我想您在你的 IdentityModels.cs

You'll need to add it to your ApplicationUser class so if you use Identity Samples, I imagine you have something like that in your 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;
    }
}

添加名字和姓氏后,它看起来像这样:

after adding First and Last Names it would look like this:

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 类中定义的

Then when you register user, you need to add them to the list now that they are defined in ApplicationUser class

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

执行迁移后,名字和姓氏将在 AspNetUsers 表中结束

first and last names will end up in AspNetUsers table after you do the migrations

这篇关于将用户名字和姓氏添加到 ASP.NET Identity 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆