在 ConfigureServices
中使用 .AddOpenIdConnect()
时,是否可以更改基于 ClientId
和 ClientSecret
在主机上从请求?
我知道 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);
};
});
}
相关链接
关于c# - ASP.NET Core 2.0 动态认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259629/