Membership cookie with subdomain v 10.4
# help-with-umbraco
r
Hi all, I have an umbraco website running on (e.g.) www.mydomain.com and a separate site running on sub.mydomain.com which logs in via an API. I've set this in my startup (above "services.AddUmbraco()" in case that's relevant) : services.ConfigureApplicationCookie(options => { options.ExpireTimeSpan = TimeSpan.FromDays(365); options.Cookie.Domain = ".mydomain.com"; }); I can see the cookie lifespan being changed to 365 days but it's ignoring the cookie domain and the cookie value is still set to www.mydomain.com. This means when I log in via sub.mydomain.com no cookie is being returned. It logs me in, but I have no way of persisting that login if the user closes their browser. Is there another way I can set the cookie domain? Is it possible to set it from the API endpoint rather than startup? Thanks in advance!
h
I think you need to do something like
Copy code
csharp
services.ConfigureApplicationCookie(options =>
{
    var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"));

    options.Cookie.Name = ".AuthCookie";
    options.Cookie.Expiration = TimeSpan.FromDays(7);
    options.LoginPath = "/Account/Login";
    options.Cookie.Domain = ".example.com";
    options.DataProtectionProvider = protectionProvider;
    options.TicketDataFormat = new TicketDataFormat(protectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Cookies", "v2"));
});
r
thank you @huwred, will give that a go. Would the API technically be a separate login path? I have a controller endpoint for www.mydomain.com which would be "/login", but the API endpoint is "api/account/login"?
c
@huwred will DirectoryInfo(@"c:\shared-auth-ticket-keys\" work on a azure webapp hosted approach rather than a server?
(I am working on this with Rob so going to try and implement this approach but as we are on webapps I wasn't sure it will work)
h
Possibly not, you may need to play around with the directory path to get it to write to the file system
c
Thanks @huwred , I think Allen's comment is for the back office cookie rather than members?
I will give it a try tomorrow and post back here/ on forum if it works 🙂
8 Views