locked
ASP.NET 3.1 : Dynamic Updating OpenID Connect Options RRS feed

  • Question

  • User-1602535186 posted

    I am having ASP.NEt 3.1 Application adn I am trying to update my OpenIDConnection options Below is the code I am trying to use I am getting error as Client ID Parameter must be provided

    public class Startup
    {
        private readonly IConfiguration _appConfiguration;
        private IApplicationBuilder _appBuilder;
        public Startup(IConfiguration configuration)
        {
          _appConfiguration = configuration;
        }
    
        public void ConfigureServices(IServiceCollection services)
        {
          services.AddAuthentication()
          .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
          .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, "Scheme1", options =>
          {
            //Wan to Set Client Id, Authority and Client Secret Dynamically
          });
          services.AddSingleton<OpenIdUpdater>();
        }
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
        {
          _appBuilder = app;
          app.UseAuthentication();
          app.UseStaticFiles();
          app.UseRouting();
          app.UseEndpoints(builder => builder.MapControllers());
        }
    }
    
      public class OpenIdUpdater
      {
        private readonly IOptionsMonitor<OpenIdConnectOptions> _openIdConnectOptionsMonitor;
        public OpenIdUpdater(IOptionsMonitor<OpenIdConnectOptions> openIdConnectOptionsMonitor)
        {
          _openIdConnectOptionsMonitor = openIdConnectOptionsMonitor;
    	  var opt = _openIdConnectOptionsMonitor.CurrentValue;
            opt.CLientID = "....";
            );
        }
      }
    
    

    Here the problem is I am getting values of client Id after Configure Services. In Configure services values are null and I want to update them later once I get the values.

    Sunday, March 29, 2020 10:57 AM

Answers

All replies

  • User475983607 posted

    This is a duplicate post.  https://forums.asp.net/p/2165387/6298813.aspx?Re+ASP+NET+3+0+ArgumentException+Options+ClientId+must+be+provided+Parameter+ClientId+

    As recommended in your other post, read the openly published Azure Ad docs for the proper configuration; https://docs.microsoft.com/en-us/aspnet/core/security/authentication/azure-active-directory/?view=aspnetcore-3.1

    Once you get the configuraiton working by reading the previous link.  Then you can look at moving the configuration to another file by creating a middleware extension method.  Your current approach does not work.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-3.1#middleware-extension-method

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1

    Sunday, March 29, 2020 11:25 AM
  • User-1602535186 posted

    It works if I pass static client Id and other options In my configureservices but with dynamic update it is not updating

    Sunday, March 29, 2020 11:30 AM
  • User475983607 posted

    TechAspirant

    It works if I pass static client Id and other options In my configureservices but with dynamic update it is not updating

    Are you trying to change the ClientId during runtime or are you trying to fetch values from configuration when the application starts?  Can you explain the use case rather than the solution?

    Sunday, March 29, 2020 11:36 AM
  • User-1602535186 posted

    In my case after configureservices gets executed. I am reviving Client Id in Configure(), So Once I received Cient Id In Configure() I have to update Connect Options. 

    Sunday, March 29, 2020 11:38 AM
  • User475983607 posted

    In my case after configureservices gets executed. I am reviving Client Id in Configure(), So Once I received Cient Id In Configure() I have to update Connect Options. 

    You still have not explained the use case clearly.  If all you are trying to do is get to the configuration when the application starts then just use the configuration member that's already available in startup. Yours is named _appConfiguration.

    Sunday, March 29, 2020 11:46 AM
  • User-1602535186 posted

    Connect Options in my case can be changed at the runtime. So if ConfigureServices is executed already How can I change or update my connect options of OpenId once changes take place.

    Sunday, March 29, 2020 11:53 AM
  • User475983607 posted

    TechAspirant

    Connect Options in my case can be changed at the runtime. So if ConfigureServices is executed already How can I change or update my connect options of OpenId once changes take place.

    You keep explaining the solution not the use case.   If you are trying to change the Azure Client ID during an HTTP request then the answer is no.  If you have multiple Azure AD application registrations then you'll need to configure each separately. 

    Maybe you are looking for a multi-tenant solution?

    https://docs.microsoft.com/en-us/azure/active-directory/hybrid/plan-connect-topologies

    Sunday, March 29, 2020 12:43 PM
  • User-1602535186 posted

    Error I am getting...I will check the Link you have shared

    [2020-03-29 16:06:57.5038][][ERROR][Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware][11]
    An unhandled exception has occurred while executing the request. Exception information:
    System.ArgumentException Options.ClientId must be provided (Parameter 'ClientId') Void Validate()    at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions.Validate()
       at Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions.Validate(String scheme)
       at Microsoft.AspNetCore.Authentication.AuthenticationBuilder.<>c__DisplayClass4_0`2.<AddSchemeHelper>b__1(TOptions o)
       at Microsoft.Extensions.Options.ValidateOptions`1.Validate(String name, TOptions options)
       at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
       at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.<Get>b__0()
       at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.get_Value()
       at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
       at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
       at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
       at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
       at IdentityServer4.Hosting.FederatedSignOut.FederatedSignoutAuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
       at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
       at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

    Sunday, March 29, 2020 2:08 PM
  • User711641945 posted

    Hi TechAsipirant,

    You could refer to the following link to check how to dynamically update OpenID connect options:

    https://stackoverflow.com/questions/60548547/how-to-change-azureadoptions-at-runtime/60558508#60558508

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 30, 2020 7:02 AM