I have followed the same approach as teh0wner (in this post : ASP.Net UserName to Email where the mail is recorded as the username which works weel so far) but when it's about updating the extra information of the user (phone number, date of birth,...) I encounter the following bug:
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 };
}
}
The error pops up at the updateAsync and mention that UserName can only be alphanumeric?!? What am I doing wrong? I don't get it, the username is already recorded as an email in the database