What is wrong with this query, why is it not returning a User object for me. I can verify that the user is successfully created, I can see the record in the DB but I am unable to query for that object so I can use that newly created UserId as FK in another table.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new {AccountType = model.AccountType });
WebSecurity.Login(model.UserName, model.Password);
var userProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
if (userProfile == null)
{
throw new Exception("no userProfile for this user");
}
if (userProfile.AccountType == AccountType.Retailer)
{
return RedirectToAction("Create", "Retailer");
}
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Here is the Enum for the AccountType
public enum AccountType
{
Retailer = 1,
Customer = 2,
Manager = 3,
Employee = 4
}
Thanks, I am looking for how retrieve either the UserProfile object or ID of a logged in user
Rather than using User.Identity.Name
, try querying using model.UserName