Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register authentication schemes based on tenant in asp.net core 3.1

Currently, I have created an Identity server 4 web application with external login providers with default client id and secrets. But my goal is to register the authentication providers like Azure, Google, Facebook based on tenant.

I have used SaasKit multi-tenancy assembly, here I have tried app.usepertenant() middleware. But UseGoogleAuthentication() method is obsolete, so i could not achieve multi-tenant authentication using this usepertenant middleware.

Current code,

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
   .AddMicrosoftAccount(option =>
     {
        option.ClientId = "clientid";
        option.ClientSecret = "clientsecret";
        option.SaveTokens = true;
     });

Expected code is like below,

var authentication = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);

if (tenant.hasMicrosoft)
{
   authentication.AddMicrosoftAccount(option =>
   {
        option.ClientId = "clientid";
        option.ClientSecret = "clientsecret";
        option.SaveTokens = true;
   });
}

if (tenant.hasGoogle)
{
   authentication.AddGoogle(option =>
   {
        option.ClientId = "clientid";
        option.ClientSecret = "clientsecret";
        option.SaveTokens = true;
   });
}

authentication.AddCookie( options =>
 {
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = new TimeSpan(7, 0, 0, 0);
 });

like image 744
Madhan kumar D Avatar asked Jan 20 '20 11:01

Madhan kumar D


People also ask

What is a tenant in authentication?

A tenant is a company with a group of users sharing common access with specific privileges. Each company has a tenant administrator. The tenant administrator has the privilege to configure settings for methods, chains, events, and so on.

How many types of authentication are there in ASP.NET Core?

ASP.NET Core API. Implementing security in a site has the following aspects: Authentication : It is the process of ensuring the user's identity and authenticity. ASP.NET allows four types of authentications: Windows Authentication.

What is authentication scheme in ASP.NET Core?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.


1 Answers

See the official MS docs, Authentication providers per tenant

ASP.NET Core framework does not have a built-in solution for multi-tenant authentication. While it's certainly possible for customers to write one, using the built-in features, we recommend customers to look into Orchard Core for this purpose.

like image 176
RickAndMSFT Avatar answered Sep 27 '22 17:09

RickAndMSFT