簡體   English   中英

ASP.NET MVC cookie不能保存?

[英]ASP.NET MVC cookies not saving?

為什么這個cookie沒有保存在我的global.asax的Session_Start方法中?

//new anon user:

var authCookie = new HttpCookie("user-id", string.Format("{0}-{1}", regiserAccountResponse.UserName, regiserAccountResponse.Key))
{
    Expires = DateTime.MaxValue,
    Domain = "domain.com",
    Secure = true,
    HttpOnly = true
};

//create the new users cookie - there's no need to call RegisterNewUserSession as this is done in the same call
HttpContext.Current.Response.SetCookie(authCookie);

如果要將cookie限制為網站的特定部分,則只需指定域。 只有在正確的范圍內,cookie才會包含在請求中。

通過將域設置為“domain.com”,您說該cookie僅對“domain.com”可用,因此您不會從localhost(或來自domain.com以外的任何其他域)檢測到它。 。

您還會注意到,如果您嘗試從您自己的域以外的域向瀏覽器發送cookie,瀏覽器將對其進行分類。

iOS現在非常熱衷於捆綁無域cookie,所以雖然接受的答案說“如果你想將cookie限制在你網站的特定部分,你只需要指定域名” - 我不認為這是真的。 您需要設置域名。

以下代碼段將從本地到生產:

    private static HttpCookie CreateCookie(string name, string value)
    {
        return new HttpCookie(name, value) { Path = "/", Expires = DateTime.UtcNow.AddYears(1), Domain = Request.Url.Host };
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM