How to implement Single Sign On in ASP.NET MVC application

594 views Asked by At

We have developed 2 applications in ASP.NET MVC. We have used ASP.NET Identity in both applications for user authentication. Both applications use the same database. Also both apps are hosted on Azure Portal.

We would like to implement SSO in both applications, so when I log in to one application, the second application doesn't ask for login again and authenticates the already logged in user.

We have implemented to share authentication cookies among ASP.NET apps as per the Microsoft document (open the link shared below), but it's not working. I have posted code below.

https://learn.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-2.1#sharing-authentication-cookies-between-aspnet-4x-and-aspnet-core-applications

I have written this code in Startup.Auth.cs:

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context, user manager and signin manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

    app.UseCookieAuthentication(new CookieAuthenticationOptions()
         {
             AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
             CookieName = ".AspNet.SharedCookie",
             CookieDomain= ".azurewebsites.net",
             SlidingExpiration = true,
             ExpireTimeSpan = TimeSpan.FromMinutes(120),
             LoginPath = PathString.FromUriComponent("/Account/Login"),
             LogoutPath = PathString.FromUriComponent("/Account/Logout"),
             TicketDataFormat = new AspNetTicketDataFormat(
                 new DataProtectorShim(
                     DataProtectionProvider.Create(new DirectoryInfo("C:\\ApplicationData"),
                             (builder) =>
                             {
                                 builder.SetApplicationName("SharedCookieApp");
                             })
                      .CreateProtector(
                             "Microsoft.AspNetCore.Authentication.Cookies." +
                             "CookieAuthenticationMiddleware",
                             "Cookies.Application",
                             "v2"))),
             CookieManager = new ChunkingCookieManager()
         });
}

Also added claim when user logged in successfully in login method as below.

  var user1 = await UserManager.FindByNameAsync(model.Email);

  var claims = new List<Claim>();
  claims.Add(new Claim(ClaimTypes.NameIdentifier, user1.Id));
  claims.Add(new Claim(ClaimTypes.Name, model.Email));
  var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);
  var ctx = Request.GetOwinContext();
  var authenticationManager = ctx.Authentication;

  authenticationManager.SignIn(new AuthenticationProperties()
  {
       AllowRefresh = true,
       IsPersistent = true,
       ExpiresUtc = DateTime.UtcNow.AddMinutes(30),
       IssuedUtc = DateTime.UtcNow
   }, id);

This works fine in localhost but when it is published in Azure portal, it doesn't work. It seems that the authentication cookie is not shared among the 2 apps on Azure Portal because Azure doesn't allow to access directory location hence it gives error like 'Access Denied' and I don't know how to use Azure Key vault or database to store cookie.

Can you please guide me if I have missed something or have did incorrectly. Your earlier response would be highly appreciated. Let me know if you need any other details.

Regards, Kiran Shah

0

There are 0 answers