我启动了一个 ASP.NET MVC 2 项目,并且正在构建自动生成的代码。
我遇到的问题是,在用户登录后,新登录用户的配置文件似乎没有加载到 中HttpContext,所以我收到一条ProviderException消息“无法为匿名用户设置此属性" 尝试在当前用户的配置文件中设置属性值时。
对于 POST-only LogOn 动作,Visual Web Developer 2010 Express 基本生成:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
//...
其中FormsService是类型控制器的属性FormsAuthenticationService(也生成):
public class FormsAuthenticationService : IFormsAuthenticationService
{
public void SignIn(string userName, bool createPersistentCookie)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}
public void SignOut()
{
FormsAuthentication.SignOut();
}
}
在这FormsService.SignIn(model.UserName, model.RememberMe)行之后,我假设新登录帐户的信息将立即可供控制器使用,但情况似乎并非如此。例如,如果我profile.SetPropertyValue("MyProfileProperty", "test")在对 FormsService#SignIn 的调用下方添加,则会得到ProviderException“无法为匿名用户设置此属性”。
如何将新登录用户的个人资料加载到 中,HttpContext以便在下一次请求之前在他或她的个人资料中设置属性值?