Breaking

Friday, September 17, 2021

ASP.NET Core: Share a cookie between subdomains

By default when you create a cookie in ASP.NET Core it is only applicable to that specific subdomain.

For example, a cookie created in subdomain.mydomain.com can not be shared with a second subdomain secondsubdomain.mydomain.com.

To change this behavior,  you need to add the following code in Startup.ConfigureServices:

services.ConfigureApplicationCookie(options =>
{
options.Cookie.Domain = ".mydomain.com";
});

By specifying a common domain in the Cookie.Domain property, the cookie will be shared between subdomain.mydomain.com and secondsubdomain.mydomain.com.

No comments:

Post a Comment