.NET 4.0 FormsAuthentication.SetAuthCookie失败

lew*_*ort 1 vb.net asp.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
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

这是我的webConfig中的Forms元素; 一切似乎对我来说......

    <authentication mode="Forms">
        <forms loginUrl="~/Public/Default2.aspx" defaultUrl="~/Public/Default2.aspx" timeout="60"/>
    </authentication>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 5

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

在您的CurrentUser方法中,您似乎正在调用该HttpContext.Current.User.Identity.Name属性,但在重定向之前,这不可用.