之前加载一个新登录的用户的个人资料,我开始一个ASP.NET MVC 2项目,我建立关闭该自动生成的代码。如何重定向
,我遇到的问题是,用户登录后,看来的新登录的用户配置文件没有加载到HttpContext
,所以我得到一个ProviderException
消息“这个属性当试图在当前用户的配置文件中设置属性值时,不能为匿名用户设置。
为后只准登录行动,的Visual Web Developer 2010速成基本产生:
[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)
线后,我是假设该信息为新登录的账户就可以立即得到控制,但是这似乎并不如此。如果,例如,我调用FormsService#签到下面添加profile.SetPropertyValue("MyProfileProperty", "test")
,然后我得到了ProviderException
“这个属性不能为匿名用户可以设置”。
如何将新登录用户的配置文件加载到HttpContext
中,以便我可以在下一个请求之前在他/她的配置文件中设置属性值?
我假设的轮廓,然后加载到Session时,“下一个”页面登录后加载。这是否正确? – 2011-01-19 16:00:12
@Nick:在这个实现中,配置文件在重定向之前立即被加载到会话中。 – 2011-01-19 16:03:16