IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Multi-tenant or dynamic ClientID - identityServer 4 and ASP.NET Core 2.0

Open ForgeEngineer opened this issue 8 years ago • 8 comments

Hi People. We have 1 Website (Client), and 1 Identity Server (Authorization Server with IdentityServer4). Our website it is multi-tenant, then our customers will do something like http://customerName.MyWebsite.com

I would like in the Client side, when I am creating the Authentication, to use as cleintID the URL, so in the Authorization server I will call ClientStore =>FindClientByIdAsync(string clientId) to search in my database the URL to bring the right information.

The problem is this:

`public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", options => { options.SignInScheme = "Cookies";

                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false; 

                options.ClientId = CONTEXT IS NULL to bring the URL; 
                options.ClientSecret = "ThisShouldBeAGoodSecret";

etc });`

Since the Context is always null in the ConfigurationService I cannot read the URL. Is it possible to create or add or edit the OpenIdConnect after the Startup.cs is called?

I read tons of examples with Core 1 about multi-tenancy, but in Core 2 does not apply those solutions, because many of the solutions are in Core 1, and are deprecated for Core 2.

Any suggestions? Ideas?

Thanks guys.

ForgeEngineer avatar Feb 28 '18 22:02 ForgeEngineer

You wouldn't change the client ID dynamically - the client is always the same.

You rather pass some extra parameter in the authorize request to indicate the tenant - this can be done using the OnRedirectingToIdentityProvider event on the handler.

leastprivilege avatar Mar 01 '18 06:03 leastprivilege

Thanks for the tip @leastprivilege I will try today to do that. My wife just deliver our baby yesterday so I wasn't able to code/read anything :) I'll let you know in a while (probably on Monda) if I was able to do your suggestion with OnRedirectingToIdentityProvide. I'm still in the Hospital until Sunday. Just one silly question, where or how I can find OnRedirectingToIdentityProvide? Thanks! --Luis

ForgeEngineer avatar Mar 02 '18 12:03 ForgeEngineer

My wife just deliver our baby yesterday

Congrats!

brockallen avatar Mar 02 '18 13:03 brockallen

My wife just deliver our baby yesterday

Congrats!

Just one silly question, where or how I can find OnRedirectingToIdentityProvider?

Have a look at this one. While being targeted to IDS3 it still applies to IDS4 https://github.com/IdentityServer/IdentityServer3/issues/841

vdecristofaro avatar Mar 04 '18 20:03 vdecristofaro

All set on this issue -- can we close?

brockallen avatar Mar 31 '18 22:03 brockallen

Sorry guys! Yes!! If any is curious I solve it by creating a Controller (I am using Razor Pages, and Core 2) and calling a StartAuthentication method (Login) in the client, and then in the acrValues I send the url.

`public async Task StartAuthentication() { //Get the hosting, these are some options //1) _context.HttpContext.Request.Host.Value = localhost:5002 //2) _context.HttpContext.Request.Host.Host = "localhost" //3) _context.HttpContext.Request.Host.Port = 5002 var host = _context.HttpContext.Request.Host.Host; // Option 2

        // read discovery document to find authorize endpoint
        var disco = await DiscoveryClient.GetAsync(Constants.Authority);
        var authorizeUrl = new RequestUrl(disco.AuthorizeEndpoint).CreateAuthorizeUrl(
            clientId: "Web.Client",
            responseType: "code id_token",
            scope: "openid profile AspCore.API offline_access",
            redirectUri: "http://localhost:5050/Account/callback",
            state: CryptoRandom.CreateUniqueId(), //"random_state",
            nonce: CryptoRandom.CreateUniqueId(), //"random_nonce",
            responseMode: "form_post",
            acrValues: host);

        return Redirect(authorizeUrl);
    }`

Thanks again.

ForgeEngineer avatar Apr 06 '18 12:04 ForgeEngineer

how to deal with tenantid on the id server side?

drowhunter avatar Dec 07 '18 12:12 drowhunter

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 12 '20 15:01 lock[bot]