I am trying to create a login app that is multi tenant. I am using ASP.NET Core Identity framework.
All I need to do I think is to have a cookie name that is different for each subdomain I access. The domain name is set to ".mydomain.com", if I could have a unique cookie name based on the URL that would be perfect.
I have been reading up on ASP.NET Core Identity but I haven't found the answer so far.
My code looks something like this:
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "<some name>";
options.Cookie.Domain "I don't know how to get the domain :-(";
...
}
...
services.AddAuthentication();
So assuming this app services https://myauth1 and https://myauth2, if myauth1 is accessed I want to be using the myauth1 cookie and if myauth2 is accessed access myauth2.
My alternative appears to be deploy a separate app for each domain, which feels very wasteful I would like to use multi tenant.
I have also tried in my Configure method and this doesn't work it doesn't get picked up in time:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<CookieAuthenticationOptions> cookieoptions, IConfiguration configuration)
{
app.Use((context, next) =>
{
cookieoptions.Value.Cookie.Domain = "my super domain";
return next.Invoke();
}
}
So that doesn't work either.
I have narrowed this down to two options:
- Either create a custom cookie authentication scheme that uses a httpContextAccessor to name the authentication cookie.
- Create some middleware that somehow renames the cookie. I found this https://hanson.io/aspnet-setting-cookies-in-middleware/ but I think option 2 will be very complex.
- Set the cookie options in middleware maybe so the cookie name can be set???
Any ideas?