无法在LogOn操作中获取成员资格用户名

时间:2012-08-03 18:20:13

标签: asp.net-mvc vb.net asp.net-mvc-3 asp.net-membership

这是我的LogOn操作:

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 个答案:

答案 0 :(得分:1)

设置身份验证cookie会将其添加到当前请求的响应中。在将响应发送到客户端之后,cookie才会出现在请求中,并且客户端会在后续请求中发送它。

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