How to configure Auth0 options out of Program.cs

I have a Blazor app and I have configured the Auth0 options in my Program.cs but what I need is to move that config out or Program.cs and do it in my controller class before the ChallengeAsync. Something like this:
[HttpGet]
public async Task ExternalLogin(string redirectUrl)
{
string callbackUrl = redirectUrl + “account/callback”;
var auth0Domain = configuration[“Auth0:Domain”];
var auth0ClientId = configuration[“Auth0:ClientId”];
var options = new OpenIdConnectOptions
{
ResponseType = “code”,
SaveTokens = true,
CallbackPath = new PathString(“/account/callback”),
Authority = $“https://{auth0Domain}”,
ClientId = auth0ClientId,
SkipUnrecognizedRequests = true
};
options.Scope.Clear();
options.Scope.Add(“openid”);
options.Scope.Add(“profile”);
options.Scope.Add(“email”);
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(callbackUrl)
.Build();

await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties, options);

}
The main idea is to change that config and no to have it static in my Program.cs.

Hi @carloslopezduranona , and welcome to the Auth0 Community!

The ChallengeAsync function does not take an options parameter and expects the HttpContext to be already configured as part of the middleware in Program.cs, as this is the intended lifecycle for a .NET Core application.

However, you could try to add multiple configurations to your pipeline in Program.cs, and decide which one to use at Controller level, based on context (such as tenant name).

Let me know if there is more you would like to know!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.