バグ:
「.www.mydomain.com」と「www.mydomain.com」に同じCookieキーを設定することがあるASP.NETWebアプリケーションがあります。ASP.NETが設定するデフォルトのCookieドメインと、誤ってサイトをコーディングして「。」を付加する方法を理解しようとしています。Cookieドメインに。
2つのCookieが同じキーを持ち、ブラウザーから送信される場合、ドメイン値がヘッダーで送信されないため、ASP.NETWebアプリケーションは2つを区別できません。(私の前の質問を参照してください)
証拠:
WebサーバーでW3Cログを有効にし、両方のCookieがクライアントから送信されることを確認しました。これがログファイルの例です(簡潔にするためにペアになっています)。
80 GET /default.aspx page= 200 0 0 - - - - - +MyCookie2=sessionID=559ddb9b-0f38-4878-bb07-834c2ca9caae;+MyCookie2=sessionID=e13d83cd-eac2-46fc-b39d-01826b91cb2c;
考えられる要因:
サブドメイン対応のフォーム認証を使用しています。
これが私のweb.config設定です:
<authentication mode="Forms">
<forms domain="mydomain.com" enableCrossAppRedirects="true" loginUrl="/login" requireSSL="false" timeout="5259600" />
</authentication>
カスタムCookieの設定例は次のとおりです。
HttpCookie cookie1 = new HttpCookie("MyCookie1") {HttpOnly = true, Expires = expiration};
logosCookie["email"] = user.Email;
logosCookie["keycode"] = user.PasswordHash;
logosCookie["version"] = currentCookieVersion;
context.Response.Cookies.Remove("cookie1");
context.Response.Cookies.Add(cookie1);
// set FormsAuth cookie manually so we can add the UserId to the ticket UserData
var userData = "UserId=" + user.UserID;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, user.Email, now, expiration, true, userData);
string str = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Secure = FormsAuthentication.RequireSSL,
Expires = ticket.Expiration
};
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
context.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
context.Response.Cookies.Add(cookie1 );
Cookieを設定する別の例を次に示します。
var cookie2 = new HttpCookie("MyCookie2");
cookie2[CookieSessionIdKey] = Guid.NewGuid();
cookie2.Expires = DateTime.Now.AddYears(10);
HttpContext.Current.Response.Cookies.Set(cookie2);
望ましくない解決策:
手動でCookieドメインを特定の値にすることはできますが、ドメインを明示的に宣言することは避けたいと思います。デフォルトのフレームワークの動作を使用し、ASP.NETの使用を変更して、「。」を付加しないようにします。カスタムCookieのCookieドメインに。