Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Identity Authentication shared between two applications

$
0
0

Hi,

I work with ASP Core 2.0

I have a first web application that I call 'Master', (localhost:5000) within this application I can log inwith Identity, which generates a Cookie. it works fine.

I have a second web application called 'Slave' (localhost:5001) whereI don't use Identity, where I would like to get Authorized with the same cookie. If the cookie is not present or expired, then the 'Slave' web site should redirect to the 'Master' login page.

Right now, if the cookie does exists from 'Master', then I try to access an Action from a Controller with the [Authorize] decoration on the 'Slave' web site : http://localhost:5001/Home/Test

I don't reach the action, in place I get redirected  to http://localhost:5001/Account/Login (so the same 'Slave' web site) where I was supposed to be redirected to http://localhost:5000/Account/Login (the 'Master' web site)

cf. in Slave Startup.cs

 options.LoginPath = "http://localhost:5000/Account/Login";

The cookie seems to be ignored.

In 'Master' web app > Startup.cs and ConfigureServices

            services.AddIdentity<ApplicationUser, IdentityRole>(config =>
                {
                    config.SignIn.RequireConfirmedEmail = true;
                    config.Password.RequireDigit = true;
                    config.Password.RequireLowercase = false;
                    config.Password.RequireUppercase = false;
                    config.Password.RequireNonAlphanumeric = false;
                    config.Password.RequiredLength = 6;
                })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddErrorDescriber<CustomIdentityErrorDescriber>()
                .AddDefaultTokenProviders();

services.AddAuthentication() .AddCookie( cfg => { cfg.SlidingExpiration = true; });

services.ConfigureApplicationCookie( options => { options.LoginPath = "/Account/LogIn"; options.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\")); } );

In 'Slave' web app > Startup.cs and ConfigureServices

            services.AddAuthentication()
              .AddCookie(
                cfg =>
                {
                    cfg.SlidingExpiration = true;
                });

            services.ConfigureApplicationCookie(
                options =>
                {options.LoginPath = "http://localhost:5000/Account/Login"; //Master web App
                    options.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"));
                }
            );

services.AddAuthentication(o => { o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; });

In 'Slave' web app > HomeController.cs

        [Authorize]
        public IActionResult Test()
        {
            return View();
        }

So I have two problems :

1) How can I reuse the Identity cookie in 'Slave' app generated with 'Master' app to avoid any log in action if the cookie from Identity does already exist ?

2) How can I be redirected to the 'Master' login page when the cookie auth  is expired when trying to access it on 'Slave' web site ? (currently redirected to the 'Slave' Login page)

Many thanks for your help

Nicolas


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>