我启动了一个.net web表单项目,该模板已经允许用户登录和注册,在管理页面上有一个重置密码的选项。如何添加更新FirstName和LastName的选项?
这是创建新用户的代码:
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text, FirstName = FirstName.Text, LastName = LastName.Text, PhoneNumber = PhoneNumber.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false); //not needed
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); //not needed
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
这就是表单的样子:
答案 0 :(得分:0)
这就是我解决它的方式:希望它可以帮助任何人。
var currentUserId = HttpContext.Current.User.Identity.GetUserId();
var context = new ApplicationDbContext();
var user = context.Users.FirstOrDefault(u => u.Id == currentUserId);
if (user != null)
{
if (FirstName.Text != "") user.FirstName = FirstName.Text;
if (LastName.Text != "") user.LastName = LastName.Text;
if (PhoneNumber.Text != "") user.PhoneNumber = PhoneNumber.Text;
if(Email.Text != "") user.Email = Email.Text;
}
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var result = userManager.Update(user);
context.SaveChanges();