如何在.Net core 2应用程序中从控制器设置OpenIdConnect Option Prompt=登录? [英] How to set OpenIdConnect option prompt ="login" from Controller in .net core 2 application?

查看:0
本文介绍了如何在.Net core 2应用程序中从控制器设置OpenIdConnect Option Prompt=登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.Net core 2应用程序,并且已在Startup.cs的ConfigureServices方法中将OpenIDConnect Options Prompt参数设置为ConfigureServices

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

但在初始登录页面中,我只想在未经同意的情况下使用提示="登录"屏幕。

在控制器页面

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

有没有办法将提示参数从控制器更改为"登录"?在以前的版本中,我们可以使用OwinContext来完成此操作。

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

感谢您的帮助。

推荐答案

可以使用Items属性传递任意参数:

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

然后您必须处理OnRedirectToIdentityProvider事件,如下所示:

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

它在项中查找是否给出了提示值,如果有,则用该提示值替换现有值。

这篇关于如何在.Net core 2应用程序中从控制器设置OpenIdConnect Option Prompt=登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆