无法在Chrome中使用和不使用www来读取/写入相同的cookie。 [英] Can not read/write the same cookie for the urls with and without www in chrome.

查看:98
本文介绍了无法在Chrome中使用和不使用www来读取/写入相同的cookie。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我的网站成为mysite.com。用户登录mysite.com然后尝试访问www.mysite.com后,他们必须再次登录。不管怎么说,无论有没有www,网址都会被视为不同。



如果有和没有www的网址,我如何读取/写入相同的cookie?



为了解决这个问题,我将我的cookie域设置为.mysite.com



在Internet Explorer中,当我登录www.mysite.com时,我也登录了mysite.com。但是它不适用于Chrome。



我做了什么:



*将域名设置为.mysite.com(已成功创建)



*给AUTHASPX以外的Cookie命名。



我写了以下代码:



Let my site be mysite.com. After users logged in mysite.com and then try visiting www.mysite.com they have to login again. Somehow url is regarded different with and without www.

How can I read/write the same cookie for the urls with and without www?

To solve this I set domain of my cookie as ".mysite.com"

In Internet Explorer, when I log-in to www.mysite.com, I also logged in to mysite.com. However it doesn't work on Chrome.

What I had done:

* Set domain of cookie as .mysite.com (It was created successfully)

* Gave a name to Cookie other than AUTHASPX.

I wrote the following code:

<br />
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(<br />
          2,                                     <br />
          username,                      <br />
          DateTime.Now,                         <br />
          rememberMe ? DateTime.Now.AddYears(1) : DateTime.Now.AddHours(1),           <br />
          rememberMe,                               <br />
          loginKey,                              <br />
          FormsAuthentication.FormsCookiePath); <br />
<br />
    // Encrypt the ticket using the machine key<br />
    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);<br />
<br />
    // Add the cookie to the request to save it<br />
    HttpCookie authCookie = new HttpCookie("CookieName", encryptedTicket);<br />
    authCookie.HttpOnly = true;<br />
    authCookie.Expires = rememberMe ? authTicket.Expiration : DateTime.Now.AddHours(1);<br />
    authCookie.Domain = ".mysite.com";<br />
<br />
    response.Cookies.Add(authCookie);<br />
<br />





认证完成通过以下代码:





Authentication is done via the following code:

<br />
HttpCookie authCookie = Context.Request.Cookies.Get("CookieName");<br />
        if (authCookie == null || string.IsNullOrEmpty(authCookie.Value))<br />
        {<br />
            LogoutAndRedirectToLogin();<br />
            return;<br />
        }<br />
<br />
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);<br />
        if (authTicket == null)<br />
        {<br />
            LogoutAndRedirectToLogin();<br />
            return;<br />
        }<br />
<br />
        string loginKey = authTicket.UserData;</pre><br />





我的尝试:



将cookie域设置为.mysite.com(已成功创建)



给AUTHASPX以外的Cookie命名。



What I have tried:

Set domain of cookie as .mysite.com (It was created successfully)

Gave a name to Cookie other than AUTHASPX.

推荐答案

看看这个链接,你会发现其他人如果你谷歌aspx表格认证子域名



c# - 跨子域的表单身份验证 - 堆栈溢出 [ ^ ]



诀窍是更改auth cookie的域,使其匹配域中的任何内容,以便www.domain.com和domain.com都能正常工作。
Look at this link, you'll find others if you google "aspx forms authentication subdomain"

c# - Forms Authentication across Sub-Domains - Stack Overflow[^]

The trick is to change the "domain" of the auth cookie so that it matches anything that ends in your domain so that "www.domain.com" and also "domain.com" both work.


这篇关于无法在Chrome中使用和不使用www来读取/写入相同的cookie。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆