本地IdentityServer4通过OpenIdConnect第一次访问站点时需要注册一个新用户。找到了在OnUserInformationReceived事件中执行此操作的“最佳位置” ,但不确定如何在事件处理程序(启动类)中访问 EF DbContext。没有用于获取 DbContext 的预配置实例(在其构造函数中请求其他依赖项)的 DI。
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
// ...
options.Events.OnUserInformationReceived = OnUserInformationReceived;
});
// ...
}
private Task OnUserInformationReceived(UserInformationReceivedContext c)
{
var userId = c.User.Value<string>(JwtRegisteredClaimNames.Sub);
// Call DbContext to insert User entry if doesn't exist.
// Or there is another place to do that?
return Task.CompletedTask;
}
动漫人物
相关分类