ASP.NET Core 2.0动态身份验证

nik*_*619 9 c# asp.net openid-connect asp.net-core asp.net-core-2.0

.AddOpenIdConnect()内部使用时ConfigureServices,是否可以根据请求更改主机ClientIdClientSecret基于主机?

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

我尝试过以下链接,但是在通过CustomAuthHandler ASP.NET Core 2.0身份验证中间件运行后,我的值始终为null

dro*_*der 5

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

在重定向到身份提供者进行身份验证之前调用。这可以用来设置ProtocolMessage.State,它将在身份验证过程中保留下来。ProtocolMessage还可以用于添加或自定义发送给身份提供者的参数。

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);
              };
         });
}
Run Code Online (Sandbox Code Playgroud)

相关链接

OpenIdConnectEvents.OnRedirectToIdentityProvider属性