Search code examples
azureopenidasp.net-core-2.0

How to set OpenIdConnect option prompt ="login" from Controller in .net core 2 application?


I'm using a .net core 2 application and have set OpenIDConnect options prompt parameter to consent in ConfigureServices method in Startup.cs

.AddOpenIdConnect(options =>
{
     options.prompt ="consent";
}

But in the initial login page I want to just use the prompt ="login" without consent screen.

In Controller page

            return Challenge(
                   new AuthenticationProperties { RedirectUri = 
                  Url.Action("Index") },
                  OpenIdConnectDefaults.AuthenticationScheme);

Is there any way to change the prompt parameter to "login" from controller. In the previous version we could do this using OwinContext.

HttpContext.GetOwinContext().Environment.Add("Prompt","login");

Any help is appreciated, thanks.


Solution

  • You can use the Items property to communicate arbitrary parameters:

    var authenticationProperties = new AuthenticationProperties
    {
        RedirectUri = Url.Action("Index")
    };
    authenticationProperties.Items["prompt"] = "login";
    return Challenge(
        authenticationProperties,
        OpenIdConnectDefaults.AuthenticationScheme);
    

    Then you will have to handle the OnRedirectToIdentityProvider event, something like this:

    options.Events = new OpenIdConnectEvents
    {
        OnRedirectToIdentityProvider = context =>
        {
            if (context.Properties.Items.TryGetValue("prompt", out string prompt))
            {
                context.ProtocolMessage.Prompt = prompt;
            }
            return Task.CompletedTask;
        }
    };
    

    It looks in the Items if there is a prompt value given, and if so, replaces the existing value with that.