ASP.NET フォーム認証を使用した Web サイトがあります。最近、ユーザーのログイン時に Cookie を保存するように実装しましたが、問題が見つかりました。問題が以前に解決されたかどうかにかかわらず、私は 100% ではありません。
再現する手順は次のとおりです。
- www (www.mysite.com) で私の Web サイトにアクセスします。
 - ウェブサイトにログインします。
 - www なしの Web サイト (mysite.com) に移動します。
 - もう一度ログインするように求められるので、ログインしました。
 - ウェブサイトからログアウトします。ログインページにリダイレクトされます。
 - アドレスバーに www.mysite.com と入力すると、まだログインしていることがわかりました。
 
したがって、(www) の有無にかかわらず、私の Web サイトにアクセスすると、2 つの異なる Web サイトにアクセスするようになります。www.mysite.com からログアウトしても、mysite.com からはログアウトされません。ログインと同じで、その逆も同様です。
ログインページ
    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
マスター ページのログイン ステータス コントロール
    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
    <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