ASP.NET - 在AuthenticateRequest()中缺少ticket.UserData(),旧工作代码安装在新服务器上

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

标签: asp.net forms-authentication

我刚刚将旧的(ASP.NET 2.0)网站迁移到新服务器。该应用程序已维护了大约8年(升级到ASP.NET 4),并在旧服务器上正常工作。它在我的开发计算机上运行良好。

本周大部分时间我一直在寻找答案,尝试了一些选择,但我仍然无法想出这个。任何帮助/指针都将不胜感激。

以下是运行代码的最相关摘要。

TIA,雷蒙德

Shared Function LogMeIn(ByVal p As Page, ByVal sUserName As String, ByVal sPassword As     String, ByVal bPersists As Boolean, ByVal lblMessage As Label) As Integer
    Try
        'get the login dataset
        Dim dsLogin As DataSet = GetUserDataSetByUserName(sUserName)

        If dsLogin.Tables(0).Rows.Count = 1 Then

            FormsAuthentication.Initialize()

            Dim sGoodPassword As String = dsLogin.Tables(0).Rows(0).Item("Password").ToString
            Dim sRole As String = dsLogin.Tables(0).Rows(0).Item("Roles").ToString

            'check password
            If sPassword = sGoodPassword Then
                Dim ticket As New FormsAuthenticationTicket(1, _
                                                            sUserName, _
                                                            DateTime.Now, _
                                                            DateTime.Now.AddMinutes(60 * 480), _
                                                            bPersists, sRole, _
                                                               FormsAuthentication.FormsCookiePath)

                Dim hash As String = FormsAuthentication.Encrypt(ticket)
                Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, hash)

                If ticket.IsPersistent Then cookie.Expires = DateTime.Now.AddDays(15)

                cookie.HttpOnly = True

                p.Response.Cookies.Add(cookie)

                Dim sReturnUrl As String = "..." 'removed for clarity

                p.Response.Redirect(sReturnUrl, True)

            Else
                lblMessage.Text = "Incorrect Password"
                Return 1
            End If
        Else
            '... 'removed for clarity
        End If
    Catch ex As Exception
        '... 'removed for clarity
    End Try

End Function


Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)

    ' Fires upon attempting to authenticate the use
    If Not (HttpContext.Current.User Is Nothing) Then

        Dim gpUser As GenericPrincipal = HttpContext.Current.User


        If gpUser.Identity.IsAuthenticated Then
            If TypeOf gpUser.Identity Is FormsIdentity Then

                Dim fi As FormsIdentity = CType(gpUser.Identity, FormsIdentity)

                Dim ticket As FormsAuthenticationTicket = fi.Ticket
                Dim sRoles As String()
                Dim i As Integer

                sRoles = ticket.UserData.Split(",")
                For i = 0 To sRoles.Length - 1
                    sRoles(i) = Trim(sRoles(i))
                Next

                HttpContext.Current.User = New GenericPrincipal(fi, sRoles)

            End If
        End If
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

问题与我使用Access数据库并且该站点最初是在ASP.NET 1.0上构建的事实有关。解决方案是实现自定义成员资格和角色提供程序。

http://www.codeproject.com/Articles/27955/Developing-custom-ASP-NET-Membership-and-Role-prov