此查询有什么问题,为什么不为我返回User对象。我可以验证用户是否已成功创建,我可以在数据库中看到该记录,但我无法查询该对象,因此我可以将新创建的UserId用作另一个表中的FK。
[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);
}
以下是AccountType的枚举
public enum AccountType
{
Retailer = 1,
Customer = 2,
Manager = 3,
Employee = 4
}
谢谢,我正在寻找如何检索UserProfile对象或登录用户的ID
答案 0 :(得分:0)
尝试使用User.Identity.Name
model.UserName