Dan*_*ien 6 c# visual-web-developer asp.net-membership asp.net-profiles asp.net-mvc-2
我启动了一个ASP.NET MVC 2项目,我正在构建自动生成的代码.
我遇到的问题是,在用户登录后,似乎新登录用户的配置文件未加载到该中HttpContext,因此我收到ProviderException消息"无法为匿名用户设置此属性"尝试在当前用户的配置文件中设置属性值时.
对于仅POST的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);
//...
Run Code Online (Sandbox Code Playgroud)
其中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();
}
}
Run Code Online (Sandbox Code Playgroud)
在FormsService.SignIn(model.UserName, model.RememberMe)我假设新登录帐户的信息可以立即供控制器使用之后,但似乎并非如此.例如,如果我在profile.SetPropertyValue("MyProfileProperty", "test")下面添加对FormsService#SignIn的调用,那么我得到ProviderException"无法为匿名用户设置此属性".
如何将新登录用户的个人资料加载HttpContext到下一个请求之前我可以在他或她的个人资料中设置属性值?
该函数的命名是反直觉的,但ProfileBase.Create(用户名)将加载现有用户的现有配置文件,或者如果不存在则创建新配置文件.
var profile = ProfileBase.Create(userName);
Run Code Online (Sandbox Code Playgroud)
这不会加载配置文件,ControllerContext.HttpContext.Profile但至少您可以访问或更改用户配置文件.
| 归档时间: |
|
| 查看次数: |
4079 次 |
| 最近记录: |