.NET 4.0 FormsAuthentication.SetAuthCookie失败

时间:2013-02-05 22:15:09

标签: asp.net vb.net forms-authentication

我知道有一些问题已经存在,但似乎没有一个问题可以解决我的问题。

我正在调试VB.NET webForms应用程序,我无法使用FormsAuthentication.SetAuthCookie工作(使用非持久性cookie)。它似乎在我在一个监视窗口中检查它时创建了一个HttpContext.Current.User对象,它似乎创建了该对象,但不是它的“Identity”属性。

我已经阅读了一些SO帖子检查了基本的东西,比如看看我的浏览器是否支持cookie等...这个项目是我们早期项目的直接端口,它使用相同的代码来处理所有事情在这里列出,相对来说它工作得很好。抛出异常的地方是从我的BLL代码调用它应该得到它。

以下是调用FormsAuthentication方法的代码......:

    'When participant logs in having already created records in DB. 
Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGo.Click
    If Me.txtUsername.Text.Trim.Length <> 0 AndAlso Me.txtPassword.Text.Trim.Length <> 0 Then
        If Membership.ValidateUser(Me.txtUsername.Text, Me.txtPassword.Text) Then
            FormsAuthentication.SetAuthCookie(Me.txtUsername.Text, False)

            'This is where we run into trouble; the property checks with the forms auth...
            MyBLL.Common.CurrentUser = New MyBLL.User(Me.txtUsername.Text)

            'set site property.. 
            If Site_ IsNot Nothing Then
                MyBLL.Common.CurrentUser.Site = Me.Site_
            End If
            MyBLL.Common.CurrentParticpant = Nothing
            MyBLL.Common.CurrentParticpantVisitID = -1
            Response.Redirect("~/Apps/Dashboard.aspx", True)
        Else
            Me.lblLoginMsg.Visible = True
        End If
    Else
        Me.lblLoginMsg.Visible = True
    End If
End Sub

这是BLL对象的代码(它有一个共享属性,从HttpContext调用用户......)

        Public Shared Property CurrentUser() As MyBLL.User
        Get
            Dim objUser As MyBLL.User

            If Not IsNothing(HttpContext.Current.Session("currentSiteUser")) Then
                objUser = CType(HttpContext.Current.Session("currentSiteUser"), MyBLL.User)
                If objUser.Username <> HttpContext.Current.User.Identity.Name Then
                    objUser = New MyBLL.User(HttpContext.Current.User.Identity.Name)
                    HttpContext.Current.Session("currentSiteUser") = objUser
                End If
            Else
                objUser = New MyBLL.User(HttpContext.Current.User.Identity.Name)
                HttpContext.Current.Session("currentSiteUser") = objUser
            End If

            Return objUser

        End Get
        Set(ByVal value As MyBLL.User)
            '_CurrentUser = value
            HttpContext.Current.Session("currentSiteUser") = value
        End Set
    End Property

这是我的webConfig中的Forms元素;一切似乎都在我身上......

    <authentication mode="Forms">
        <forms loginUrl="~/Public/Default2.aspx" defaultUrl="~/Public/Default2.aspx" timeout="60"/>
    </authentication>

1 个答案:

答案 0 :(得分:5)

在密封SetAuthCookie方法之后,您应该立即重定向,并且仅在后续请求中您可能希望初始化完整的IPrincipal。不要尝试在调用HttpContext.Current.User.Identity.Name方法的同一控制器操作中访问SetAuthCookie。它不会有任何影响。重定向很重要,因此在下一个请求中,表单身份验证模块将从请求cookie构建主体。

在您的CurrentUser方法中,您似乎在调用HttpContext.Current.User.Identity.Name属性,但在重定向之前无法使用此属性。