Hi, I am using identitiy framework to my existing application (I added it after I start working with the login).
Generally, I was using my own database. Right Now, I have the user inserted in 2 tables 1- my own table 2- aspnetUsers table.
As a resule, when I try to update or display user profile I use the identity user to generate that information.
However, when the user login and try to access the profile view, I'm getting an error that the "UserName" parameter cann't be null.
How Would I generate the user information in this case?
My action function looks as the following:
- [HttpGet]
- public async Task <ActionResult> UpdateDriverProfile()
- {
-
- var user = User.Identity.Name;
- var userStore = new UserStore<IdentityUser>();
- var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(new RidesDbContext()));
-
-
-
- DriversModel driver = new DriversModel();
- IdentityUser theUser = new IdentityUser()
-
- {
- UserName = driver.email,
- Email = driver.email
- };
-
- driver.email = theUser.UserName;
- driver.email = theUser.Email;
- driver.phoneNumber = theUser.PhoneNumber;
- driver.PasswordHash = theUser.PasswordHash;
- driver.userID = theUser.Id;
-
- var store = await userStore.FindByNameAsync(theUser.UserName);
- theUser = await userManager.FindByNameAsync(driver.email);
-
-
- DriversTier tier = new DriversTier();
- driver = tier.getDriverByUserID(driver.userID);
- return View("UpdateDriverProfile", driver);
-
- }
just to make things clear, this is how I rwgister or add the user to the database:
-
- [HttpPost]
- public async Task<ActionResult> DriversRegistration(DriversModel driver, IEnumerable<HttpPostedFileBase> images)
- {
- var profileImg = images.ElementAt(0);
- var licenseImg = images.ElementAt(1);
-
-
- if (profileImg != null && profileImg.ContentLength > 0)
- {
- driver.profileImgType = profileImg.ContentType;
- string fileName = profileImg.FileName;
- string pattern = @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";
-
- Regex rgx = new Regex(pattern);
-
-
- driver.profileImg = new byte[profileImg.ContentLength];
-
-
- profileImg.InputStream.Read(driver.profileImg, 0, profileImg.ContentLength);
-
-
-
- ModelState.Clear();
-
- }
-
- if (licenseImg != null && licenseImg.ContentLength > 0)
- {
- driver.licenseImgType = licenseImg.ContentType;
- string fileName = licenseImg.FileName;
- string pattern = @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";
-
- Regex rgx = new Regex(pattern);
-
-
- driver.licenseImg = new byte[licenseImg.ContentLength];
-
-
- licenseImg.InputStream.Read(driver.licenseImg, 0, licenseImg.ContentLength);
-
-
-
- ModelState.Clear();
-
- }
-
- if (ModelState.IsValid)
- {
-
- bool processError = false;
- var userStore = new UserStore<IdentityUser>();
- var userManager = new UserManager<IdentityUser>(userStore);
- string statusMessage = "";
- IdentityUser theUser = new IdentityUser() {
- UserName = driver.email,
- Email = driver.email,
- PasswordHash = driver.PasswordHash,
- PhoneNumber = driver.phoneNumber
-
- };
- IdentityResult theResult = await userManager.CreateAsync(theUser,driver.PasswordHash);
- var store = await userStore.FindByNameAsync(theUser.UserName);
- theUser = await userManager.FindByNameAsync(driver.email);
- string UserName = theUser.UserName;
- string password = theUser.PasswordHash;
- string Email = theUser.Email;
- string phoneNumber = theUser.PhoneNumber;
- string userID = theUser.Id;
- driver.userID = userID;
- DriversTier tier = new DriversTier();
- int newID = tier.insertDriver(driver);
- driver.driverId = newID;
-
- if (theResult == IdentityResult.Success)
- {
-
-
- if (driver != null)
- {
- var roleStore = new RoleStore<IdentityRole>();
- var roleManager = new RoleManager<IdentityRole>(roleStore);
- var therole = new IdentityRole("Driver");
- theResult = await roleManager.CreateAsync(therole);
-
- if (theResult == null)
- {
- statusMessage = string.Format("User Group Creation failed because : {0}", theResult.Errors.FirstOrDefault());
-
- processError = true;
- }
- }
-
- if (!processError)
- {
-
- theResult = await userManager.AddToRoleAsync(theUser.Id, "Driver");
- statusMessage = string.Format("Identity User {0} was created successfully!<br /> {0} was added to the User group: {1}", theUser.UserName, theResult.Errors.FirstOrDefault());
- }
-
- }
- else
- {
-
- statusMessage = string.Format("User Creation failed because : {0}", theResult.Errors.FirstOrDefault());
- processError = true;
- }
-
- List<IdentityUser> userList = userManager.Users.ToList<IdentityUser>();
- return RedirectToAction("ThankYou", "Default");
- }
- else
- {
- return View();
- }
and this is the model I'm using for both registration and the profile:
- public class DriversModel
- {
- [Key]
- [Display(Name ="Id")]
- public int driverId { get; set; }
-
- public string userID { get; set; }
-
- [Display(Name = "First Name")]
- [Required(ErrorMessage = "Pleas Enter Your First Name")]
- public string firstName { get; set; }
-
- [Display(Name = "Last Name")]
- [Required(ErrorMessage = "Pleas Enter Your Last Name")]
- public string lastName { get; set; }
-
- [Display(Name = "Email Address")]
- [DataType(DataType.EmailAddress)]
- [Required(ErrorMessage = "Pleas Enter Your Email Address")]
- [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please Enater a Valid Email Address")]
- public string email { get; set; }
-
- [Display(Name = "Mobile Number")]
- [Required(ErrorMessage = "Pleas Enter Your Mobile Number")]
- public string phoneNumber { get; set; }
-
- [Display(Name = "Address")]
- [Required(ErrorMessage = "Pleas Enter Your Address")]
- public string Address { get; set; }
-
- [Display(Name = "City")]
- [Required(ErrorMessage = "Pleas Enter Your City")]
- public string city { get; set; }
-
- [Display(Name = "State")]
- [Required(ErrorMessage = "Pleas Enter Your state")]
- public string state { get; set; }
-
- [Display(Name = "Car")]
- [Required(ErrorMessage = "Please Identify Your Car")]
- public string car { get; set; }
-
- [Display(Name = "Driver's License")]
- [Required(ErrorMessage = "Please Enter Your Driver's Licende Number")]
- public string driverslicense { get; set; }
-
-
- [Display(Name ="Profile Image")]
- [Required]
- public byte[] profileImg { get; set; }
-
- public string profileImgType { get; set; }
-
- [Display(Name = "License Image")]
- [Required]
- public byte[] licenseImg { get; set; }
-
- public string licenseImgType { get; set; }
-
- [Display(Name ="Password")]
- [DataType(DataType.Password)]
- [Required(ErrorMessage ="Please Enter a password")]
- public string PasswordHash { get; set; }
-
- [Display(Name = "Username")]
- public string UserName { get; set; }
-
-
- }
-
- }
I would appreciate your help. Thank You.