用户首次使用OpenId Connect登录后,将新UserId放入数据库的位置在哪里?

时间:2018-07-24 08:01:17

标签: c# openid-connect asp.net-core-2.1

首次注册新用户时,需要由本地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;
}

1 个答案:

答案 0 :(得分:2)

UserInformationReceivedContext类包含一个HttpContext属性,该属性本身包含一个RequestServices属性。此lens = len(df) df['probit_pdf'] = df.groupby('Value')['Value'].transform('size').div(lens) df['probit_cdf'] = df['probit_pdf'].apply(lambda x: df['probit_pdf'].ge(x).sum()).div(lens) print (df) Id Value probit_pdf probit_cdf 0 1 2 0.4 0.4 1 2 4 0.3 0.7 2 3 2 0.4 0.4 3 4 6 0.1 1.0 4 5 5 0.2 0.9 5 6 4 0.3 0.7 6 7 2 0.4 0.4 7 8 4 0.3 0.7 8 9 2 0.4 0.4 9 10 5 0.2 0.9 属性的类型为IServiceProvider,可用于访问在依赖注入容器中注册的服务。

下面是使用GetService<T>的示例:

RequestServices