使用Asp.Identity将用户名更新为邮件错误

时间:2014-06-06 16:08:39

标签: asp.net email identity username

我采用了与teh0wner相同的方法(在这篇帖子中:ASP.Net UserName to Email,其中邮件被记录为目前为止工作的用户名)但是当它是关于更新用户的额外信息时(电话号码,出生日期......)我遇到以下错误:

My update code:

    UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(new          ApplicationDbContext());
    UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(store);
    UserValidator<ApplicationUser> validator = new UserValidator<ApplicationUser>(manager);
    var updateUser = new ApplicationUser
    {
        UserName = User.Identity.GetUserName(),
        Id = User.Identity.GetUserId(),
        FirstName = FirstName.Text,
        MiddleName = MiddleName.Text,
        LastName = LastName.Text,
        Title = Convert.ToInt32(DropDownList_Title.SelectedValue),
        BirthDate = DateOfBirth
    };

    var result = manager.UpdateAsync(updateUser);
    //IdentityResult result = await UserManager.UpdateAsync(updateUser);

    store.Context.SaveChanges();

My model (IdentityModels.cs):


public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public Int32 Title { get; set; }
    public string PhoneNumber { get; set; }
    public DateTime BirthDate { get; set; }
    public string Language { get; set; }
}
public class UserManager : UserManager
{
    public UserManager()
        : base(new UserStore(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator(this) { AllowOnlyAlphanumericUserNames = false };
    }
}

updateAsync会弹出错误,并提到UserName只能是字母数字?!?我究竟做错了什么?我没理解,用户名已经记录为数据库中的电子邮件

1 个答案:

答案 0 :(得分:0)

非常感谢。 “.Result”刚刚失踪。

 UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(new ApplicationDbContext());
 UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(store);
 ApplicationUser user = manager.FindByNameAsync(User.Identity.Name).Result;

...

var result = manager.UpdateAsync(user);
store.Context.SaveChanges();