表单身份验证Cookie不在服务器上保留但在LocalHost上保留

时间:2013-02-25 20:36:45

标签: asp.net iis-7 forms-authentication persistent

我一直致力于在IIS 7上设置的Web应用程序,作为在DefaultWebSite下运行的多个应用程序和Web服务之一。在localhost上开发时,我的FormsAuthentication cookie仍然存在;但是,当在服务器上发布时,cookie不是持久的。我正在使用SQLServer会话,并在Web配置中具有验证密钥以排除应用程序池回收。我一直在努力解决这个问题一段时间没有运气......请帮忙!

以下是一些代码:

<forms name=".OPTFORMSTEST" loginUrl="~/Secure/Login.aspx" defaultUrl="~/Default.aspx"  timeout="240" path="/" slidingExpiration="false" protection="All" />

Public Shared Function DoLogin(ByVal strUsername As String, ByVal isPersistent As Boolean)
    Dim authTicket As FormsAuthenticationTicket
    Dim authCookie As HttpCookie
    Dim strUserData As String = strUsername
    Dim intTimeoutPersist As Integer = 43200    '(30 days)
    Dim intTimeoutNonPersist As Integer = 300   '(5 hours)
    Dim intRtn As Integer = 1
    Dim strCookiePath As String = Current.Request.Url.AbsolutePath.Remove(Current.Request.ApplicationPath.Length)

    Try
        'set cookie timout period and create auth ticket based on isPersistent
        If isPersistent Then
            'create a persistent ticket
            authTicket = New FormsAuthenticationTicket(1, strUsername, _
                DateTime.Now(), _
                DateTime.Now.AddMinutes(intTimeoutPersist), _
                True, strUserData)
        Else
            'create a temp ticket
            authTicket = New FormsAuthenticationTicket(1, strUsername, _
                DateTime.Now(), _
                DateTime.Now.AddMinutes(intTimeoutNonPersist), _
                False, strUserData)
        End If

        'create encrypted string for user data
        Dim strEncr As String = FormsAuthentication.Encrypt(authTicket)
        'create cookie
        authCookie = New HttpCookie("OPTFORMSTEST", strEncr)
        'set cookie expiration based on the auth ticket
        If isPersistent Then
            authCookie.Expires = authTicket.Expiration
        End If
        Current.Response.Cookies.Add(authCookie)
    Catch ex As Exception
        intRtn = -1
    End Try
    Return intRtn
End Function

1 个答案:

答案 0 :(得分:0)

经过一些实验,我发现RedirectFromLoginPage方法在从生产框中使用时创建了另一个cookie。我用Response.Redirect替换了,只创建了一个cookie并且它是持久的。