Don Porter
Don Porter

Reputation: 1

Merging two ASP.NET MVC projects together which use ASP.NET Identity

We have two ASP.NET MVC projects which both use ASP.NET Identity, and we need to merge them into one single project. We've got a solution made which contains both projects, and they both use the same database for login information and the same UserStores, etc. We need to be able to redirect from the controller on one project to views on the other and have login credentials persist between both projects. We've got the solution set to start both projects simultaneously when debugging and they are both loading properly, but logins are not persisting between the two and we cannot jump from a view on one to a view on the other. Is this at all possible or are we better off merging the two projects into one single project?

Upvotes: 0

Views: 1160

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239460

Generally, I would say, yes, you're better off merging the two projects into one, but you don't necessarily have to. Sharing auth between projects requires two things:

  1. The projects must be on the same primary domain. They can be on subdomains, i.e project1.domain.com and project2.domain.com, but they cannot be on entirely different domains, i.e. project1.com and project2.com.

    If they are on subdomains, you'll need to use a wildcard cookie. Find the line in Startup.Auth.cs:

    app.UseCookieAuthentication(new CookieAuthenticationOptions {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        ...
    });
    

    One of the properties of CookieAuthenticationOptions that you can initialize is CookieDomain. Set it to ".yourdomain.com".

  2. They must have the same machine key set in the both their Web.config's. See: https://msdn.microsoft.com/en-us/library/w8h3skw9(v=vs.100).aspx

Upvotes: 1

Related Questions