Search code examples
authenticationasp.net-core-mvcasp.net-core-2.0asp.net-core-mvc-2.0

Share credentials between 2+ internal MVC Core 2 apps w/o Identity Server


What is the best way to share credentials between 2+ ASP.Net MVC Core applications, meaning a user can log into one application and will have access to the other application(s). The apps are for external clients but would be hosted on the same server. I am looking for the simplest solution, I don't want to implement an OAuth server unless absolutely necessary. I have considered the following options:

  1. Implement Identity Server 4. This seem like overkill for my use case, looks like this is a significant effort, and may require more time to implement than a simple MS Core Identity solution.
  2. Use MVC Core 2 MS Identity and share the authentication database between the two databases. This implies using a unique DB for each application, but a single DB for authentication.
  3. Sharing authentication cookies between applications (on the same domain etc).
  4. Any other ideas?

Solution

  • It depends on one main thing: what are the domains the sites are being deployed to? Session-based authentication (what websites use) depends on a cookie being set on the client. Importantly, cookies are domain-bound. In other words, a cookie can only be "shared" with sites on the same domain, or subdomains on the same domain if the cookie domain is set as a wildcard.

    That right there defines your whole approach. If you've got something like abc.com/site1 and abc.com/site2 or even site1.abc.com and site2.abc.com, you can share the cookie. However, if you're dealing with abc.com and xyz.com, you're borked.

    If all the sites are on the same domain (or subdomains on the same domain) then all you need to ensure is that all the sites are using the same data protection setup. In previous ASP.NET sites, encryption was handled via a "machine key", so you simply had to ensure that the machine key was shared. ASP.NET Core uses Data Protection which functions differently, but the basic principle still exists. All the setup needs to be the same across all the sites so they essentially all encrypt and decrypt things in the same way. Since the auth cookie is encrypted, this is absolutely required to shared that cookie. Thankfully, Microsoft has a guide for doing this.

    If the sites are on completely different domains, there is no way to share the auth cookie. Period. Full stop. Your only choice then is something like Identity Server.