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

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

问题描述

我改用了新的ASP.NET Identity2.实际上,我使用的是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表中

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

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