我正在尝试从MVC4中使用SimpleMembershipProvider的UserProfile中获取自定义属性。我的表格如下:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
//Added to round out requirements for site user profile
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
当我尝试在登录时的会话变量中获取我感兴趣的userprofile项目,FirstName,
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
var context = new UsersContext();
var curuser = context.UserProfiles.First(p => p.UserName == User.Identity.Name);
Session["FName"] = curuser.FirstName;
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
我收到错误:Sequence没有包含我的var curuser行的元素。如果我用model.Username替换User.Identity.Name,它可以正常工作。我宁愿使用User.Identity.Name,因为我可以在任何需要的地方使用它,或者我认为。