c# - ASP.NET Core 2.0 动态认证

标签 c# asp.net asp.net-core asp.net-core-2.0 openid-connect

ConfigureServices 中使用 .AddOpenIdConnect() 时,是否可以更改基于 ClientIdClientSecret在主机上从请求?

我知道 Startup 本身没有访问 HttpContext 的权限,但我想知道使用中间件是否可以解决这个它可以访问上下文的问题.

我试过点击下面的链接,但是在它通过 CustomAuthHandler 运行后我的值总是空的 ASP.NET Core 2.0 authentication middleware

最佳答案

我相信您可以实现将函数分配给 RedirectToIdentityProvider 属性的目标。

Invoked before redirecting to the identity provider to authenticate. This can be used to set ProtocolMessage.State that will be persisted through the authentication process. The ProtocolMessage can also be used to add or customize parameters sent to the identity provider.

public void ConfigureServices(IServiceCollection services)
{
    services
    .AddAuthentication()
    .AddOpenIdConnect(options =>
        {
            options.Events.OnRedirectToIdentityProvider = context =>
             {
                  // Retrieve identity from current HttpContext
                  var identity = context.HttpContext.User.Identity;

                  // Lookup for your client_id and client_secret
                  var clientId = "find your client id";
                  var clientSecret = "find your client secret";

                  // Assign client_id and client_secret
                  context.ProtocolMessage.ClientId = clientId;
                  context.ProtocolMessage.ClientSecret = clientSecret;

                  return Task.FromResult(0);
              };
         });
}

相关链接

OpenIdConnectEvents.OnRedirectToIdentityProvider Property

关于c# - ASP.NET Core 2.0 动态认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259629/

相关文章:

c# - 嵌套 while 循环替代方案

c# - 使用 LINQ 包含 - 如何确保后面没有特定字符

c# - 将代码中的数据库表首先转换为模型的最佳方法

git - ASP.NET Core - 使用 git 部署在 Azure 上运行 webpack

c# - 创建和删除图片/屏幕截图

javascript - 计算距离并从谷歌地图中的原点移动标记

javascript - 不要使用后退按钮让弹出窗口出现

asp.net-core - 将 Blazor 组件放置在用户内容内

json - Microsoft.Extensions.Configuration 只读?!?!真的吗?

c# - 是否可以在类成员函数中使用 switch 语句并获取访问器?