本地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;}
用戶首次使用 OpenId Connect 登錄后,將新 UserId 放入數據庫的位置在哪里?
慕田峪7331174
2021-10-24 17:54:58