我刚刚将旧的 (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