Share cookies between subdomains is a common requirement in web development. For example, you have a website with multiple subdomains, and you want to share the login status between these subdomains. Once a user logs in to one subdomain, the user should be logged in to all subdomains.
Sharing cookies between subdomains is a common requirement in web development. For example, you have a website with multiple subdomains, and you want to share the login status between these subdomains. Once a user logs in to one subdomain, the user should be logged in to all subdomains.
This article will show you how to achieve this in an ASP.NET Core application.
@ -11,13 +11,13 @@ This article will show you how to achieve this in an ASP.NET Core application.
The `cookie` has a `Domain` attribute which specifies which server can receive a cookie.
If specified, then cookies are available on the server and its subdomains. For example, if you set `Domain=.abp.io`, cookies are available on `abp.io` and its subdomains like `community.abp.io`.
If the server does not specify a Domain, the cookies are available on the server but not on its subdomains. Therefore, specifying Domain is less restrictive than omitting it. However, it can be helpful when subdomains need to share information about a user.
If the server does not specify a **Domain**, the cookies are available on the server but not on its subdomains. Therefore, specifying the **Domain** is less restrictive than omitting it. However, it can be helpful when subdomains need to share information about a user.
## Change the domain of the cookie in ASP.NET Core
There is a `CookiePolicyMiddleware` in ASP.NET Core, You can add some policies to the `CookiePolicyOptions` during cookies are appended or deleted.
There is a `CookiePolicyMiddleware` in ASP.NET Core, you can add some policies to the `CookiePolicyOptions` during cookies are appended or deleted.
We will add a policy to the `CookiePolicyOptions` to change the `domain` of the cookie.
We will add a policy to the `CookiePolicyOptions` to change the `domain` of the cookie:
The subdomains can share the `.AspNetCore.Culture` cookie now.
In another community article we use same middleware to [fix the Chrome login issue for the IdentityServer4](https://community.abp.io/posts/patch-for-chrome-login-issue-identityserver4-samesite-cookie-problem-weypwp3n)
In another community article, we use the same middleware to [fix the Chrome login issue for the IdentityServer4](https://community.abp.io/posts/patch-for-chrome-login-issue-identityserver4-samesite-cookie-problem-weypwp3n)
## Summary
The `CookiePolicy` middleware provides a way to control cookies in an ASP.NET Core, It very useful if you have more complex requirements for Cookies.
The `CookiePolicy` middleware provides a way to control cookies in an ASP.NET Core, It is very useful if you have more complex requirements for Cookies.