2012-04-25 150 views
1

我有一個ASP.NET窗體身份驗證的網站。我最近實現了在用戶登錄時保存cookie,現在我發現了一個問題。如果問題持續存在或不存在,我不是100%。網站在登出後仍然登錄

複製的步驟是:

  1. 去我的網站WWW(www.mysite.com)
  2. 登錄該網站。
  3. 去無網站的網站(mysite.com)
  4. 它會問我再次登錄,所以我做了。
  5. 註銷網站。它將我重定向到登錄頁面。
  6. 在地址欄輸入www.mysite.com,我發現它仍然登錄。

因此,訪問我的網站有或沒有(www)變得像訪問兩個不同的網站。從www.mysite.com註銷不會從mysite.com註銷。與登錄相同,反之亦然。在母版頁

LoginStatus1_LoggingOut Handles LoginStatus1.LoggingOut 

    FormsAuthentication.SignOut() 
    Session.Clear() 
    Session.Abandon() 
    Dim cookie1 As New HttpCookie(FormsAuthentication.FormsCookieName, "") 
    cookie1.Expires = DateTime.Now.AddYears(-1) 
    Response.Cookies.Add(cookie1) 

    Dim cookie2 As New HttpCookie("ASP.NET_SessionId", "") 
    cookie2.Expires = DateTime.Now.AddYears(-1) 
    Response.Cookies.Add(cookie2) 

的Web.config

登錄頁面

Login1_Authenticate Handles Login1.Authenticate 

    Dim result As Boolean = UserLogin(userName, password) 
    If (result) Then 
     e.Authenticated = True 
     If Login1.RememberMeSet = True Then 
      SetCookies(userName) 
     End If 
     LoginCounter(userName) 
    Else 
     e.Authenticated = False 
    End If   

SetCookies()

Dim tkt As FormsAuthenticationTicket 
    Dim cookiestr As String 
    Dim ck As HttpCookie 

    tkt = New FormsAuthenticationTicket(1, userName, DateTime.Now(), DateTime.Now.AddDays(7), False, "") 
    cookiestr = FormsAuthentication.Encrypt(tkt) 
    ck = New HttpCookie(FormsAuthentication.FormsCookieName(), cookiestr) 
    ck.Expires = tkt.Expiration 
    ck.Path = FormsAuthentication.FormsCookiePath() 
    HttpContext.Current.Request.Cookies.Remove(".ASPXAUTH") 
    Response.Cookies.Add(ck) 

    End Sub 

登錄狀態控制

<authorization> 
    <deny users="?"/> 
    </authorization> 

    <authentication mode="Forms"> 
    <forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="Default.aspx" cookieless="UseCookies" timeout="1440" path="/" protection="All"/> 
    </authentication> 

解決方案:把這個Global.asax中..

 Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) 
    Dim fromurl As String = "http://mysite.com" 
    Dim tourl As String = "http://www.mysite.com" 
    If HttpContext.Current.Request.Url.ToString().ToLower().Contains(fromurl) Then 
     HttpContext.Current.Response.Status = "301 Moved Permanently" 
     HttpContext.Current.Response.AddHeader("Location", tourl) 
    End If 
    End Sub 

回答

1

我想說的會話cookie(子)域特定的。

您需要將所有請求從一個域重定向到另一個域,以強制瀏覽器僅使用一個會話。

+0

Okie ....但如何做到這一點? – lawphotog 2012-04-25 21:00:51

+0

在您的主頁上,如果'HttpContext.Current.Request.ServerVariables [ 「HTTP_HOST」]'匹配一個域, '的Response.Redirect( 「http://www.otherdomain.com」,假);' 或 'response.RedirectPermanent(「http://www.otherdomain.com」,false);'如果你想谷歌只索引你的其他域。 – gcochard 2012-04-25 21:07:11

+0

謝謝你會嘗試:) – lawphotog 2012-04-25 21:08:24