Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 642
bob Avatar asked Apr 26 '18 07:04

bob


People also ask

What is CallbackPath signin OIDC?

The CallbackPath represents the URL to which the browser should be redirected to and the default value is /signin-oidc. The picture below shows how it is related: Follow this answer to receive notifications. answered Dec 20, 2021 at 12:31.

What does signin OIDC mean?

OpenID Connect (OIDC) extends the OAuth 2.0 authorization protocol for use also as an authentication protocol. You can use OIDC to enable single sign-on (SSO) between your OAuth-enabled applications by using a security token called an ID token.

How do I add Azure AD authentication to existing ASP NET core application?

Select ASP.NET Core Web Application>Choose Web Application (Model-View-Controller) template> Click on the "Change Authentication" button>Select "Work or School Accounts". Choose Cloud - Single Organization. Fill up the field of Domain which is the Azure Active Directory tenant name (say, softdreams.onmicrosoft.com).

What is OIDC configuration?

Openid-configuration is the OpenID Connect Provider's discovery document. The discovery document describes the provider's API endpoints used during the authentication sequence. Specifying this URL connects the authenticator to a particular OpenID Connect Provider.


1 Answers

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.

like image 51
juunas Avatar answered Sep 17 '22 04:09

juunas