Search code examples
asp.netasp.net-mvcasp.net-mvc-5asp.net-identity

Using the same login in two projects with ASP.NET MVC5 Identity


I'm trying to share the same login cookie to two differents MVC5 projects.

I've already set the same cookie name to both projects, but thats doesnt work.

 app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            CookieName = "OrienteIntegrada",
            Provider = new CookieAuthenticationProvider
            {
                // Permite que o aplicativo valide o carimbo de segurança quando o usuário efetuar login.
                // Este é um recurso de segurança que é usado quando você altera uma senha ou adiciona um login externo à sua conta.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });         

Both apps are in the same site... and I can get them by accessing localhost/teste and localhost/teste2;

Am I supposed to set anything in IIS? I'm sorry about my english... I hope you may understand me. Thx


Solution

  • Use the same machinekey for the both applications and they will share the secure data. Here is how you can add machinekey to the web.config:

    <configuration>
        <system.web>
            <machineKey decryptionKey="F6722806843145965513817CEBDECBB1F94808E4A6C0B2F2,IsolateApps" validationKey="C551753B0325187D1759B4FB055B44F7C5077B016C02AF674E8DE69351B69FEFD045A267308AA2DAB81B69919402D7886A6E986473EEEC9556A9003357F5ED45,IsolateApps" />
        </system.web>
    </configuration>
    

    Do not use the above values on production. Only use on your test applications.

    You can generate machine key following the instructions from here: https://learn.microsoft.com/en-us/archive/blogs/vijaysk/iis-7-tip-10-you-can-generate-machine-keys-from-the-iis-manager