Sharing ASP.NET cookies across sub-domains

前端 未结 4 1943
野趣味
野趣味 2020-12-01 03:47

I have two sites, both on the same domain, but with different sub-domains.
site1.mydomain.com site2.mydomain.com

Once I\'m authenticated on each, I look at the c

4条回答
  •  [愿得一人]
    2020-12-01 04:22

    Add new cookie and specify domain like this

    HttpCookie cookie = new HttpCookie("cookiename", "value");
    cookie.Domain = "domain.com";
    

    For forms authentication set this in web.config

    
    
    

    The cookie will be accessible to all the subdomains.

    In order for each domain to decrypt the the cookie, all web.config files must use the same encryption/decryption algorithm and key. (how to create a machine key)

    Example:

    // do not wrap these values like this in the web.config
    // only wrapping for code visibility on SO
    
    

    For easier deployments, these values can be stored in a separate file:

    
    

    For added security you can also encrypt the machine key for further protection..

提交回复
热议问题