如何添加用户名和姓到ASP.NET身份2? [英] How can I add user First Name and Last Name to an ASP.NET Identity 2?
问题描述
我改变了使用新的ASP.NET身份2.我实际上使用Microsoft ASP.NET身份样本2.0.0-β2。
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.
谁能告诉我在哪里,因此它存储用户名和姓与用户细节一起我如何修改code。请问这是现在索赔的一部分,如果是这样我怎么能增加吗?
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样品,我想象你有类似的东西,你的 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
定义的类
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身份2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!