2012-08-03 21 views
1

这里了登录操作:不能办理会员用户名根据登录行动

Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult 
    If ModelState.IsValid Then 
     If Membership.ValidateUser(model.UserName, model.Password) Then 
      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe) 
      If Url.IsLocalUrl(returnUrl) AndAlso returnUrl.Length > 1 AndAlso returnUrl.StartsWith("/") _ 
       AndAlso Not returnUrl.StartsWith("//") AndAlso Not returnUrl.StartsWith("/\\") Then 
       Return Redirect(returnUrl) 
      Else 

       ' redirect based on whether the user is admin, company owner, or blog owner 

       Debug.Print(Membership.GetUser.UserName) 

当我尝试调试变量,它说,“NullReferenceException异常,未将对象引用设置到对象的实例。 “

这只是一分钟前的工作!我怎样才能获得用户名变量?谢谢。

回答

1

设置认证cookie将其添加到当前请求的响应中。直到响应发送给客户端,并且客户端在随后的请求中发送该请求之前,cookie还没有出现在请求中。

好消息是,你已经有模型的用户名,所以我建议使用它。

+0

这工作,并很好的建议。然而,我很好奇,为什么它工作之前,突然停止...我使用Membership.GetUser.UserName,它给我的用户名! – user1477388 2012-08-03 18:28:42

+0

Cookie可能已经被发送,但随后被清除或更改? – Dan 2012-08-03 18:31:37

+0

我认为这是可能的,但我不确定。谢谢你的帮助,丹。 – user1477388 2012-08-03 18:37:09