简体   繁体   English

尝试在 asp.net 核心中首次使用 Auth0 注册后将用户数据保存到 dbcontext

[英]Trying so save user data to dbcontext after first signup with Auth0 in asp.net core

What I am trying to achieve: 1. Getting all the claims after OnTicketReceived event from Auth0.我想要实现的目标: 1. 从 Auth0 获取 OnTicketReceived 事件之后的所有声明。 2. Checking if the user is signing up for first time (bool value in 1 of the claims). 2. 检查用户是否是第一次注册(声明中的布尔值)。 3. Saving new user object into localdb (if it's the first time logging in). 3. 将新用户 object 保存到 localdb(如果是第一次登录)。 Currently I am stuck on third step.目前我被困在第三步。 In order to use OnTicketReceived I need a static method on the right side but then I cannot use DbContext in my controller class because it is not a static variable. In order to use OnTicketReceived I need a static method on the right side but then I cannot use DbContext in my controller class because it is not a static variable.

Startup.cs启动.cs

services.AddAuthentication(options =>
            ...
            .AddOpenIdConnect("Auth0", options =>
            {
                options.Scope.Add("openid");
                ...
                options.Events = new OpenIdConnectEvents
                {
                    OnTicketReceived = UsersController.CreateOnSignUp,
                ...
                };
            });

UsersController.cs用户控制器.cs

        public static async Task<Task> CreateOnSignUp(TicketReceivedContext ticketReceivedContext)
        {
            List<Claim> claims = ticketReceivedContext.Principal.Claims.ToList();
            var claim = claims.FirstOrDefault(x => x.Type.EndsWith("isNewUser"));
            bool isNewUser = bool.Parse(claim.Value);

            if (isNewUser)
            {
                string userOId = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
                User user = new User() { ExternalId = userOId };
                //insert new value into DB
                //_context.Add(user);
                //await _context.SaveChangesAsync();
            }

            return Task.CompletedTask;
        }

Commented code lines are currently not working.注释的代码行当前不起作用。 Could anyone recommend a better way of saving user data to DB after authentication or a workaround to my current solution?任何人都可以推荐一种在身份验证后将用户数据保存到数据库的更好方法或我当前解决方案的解决方法吗?

First of all, the controllers are instantiated per call (or for each HTTP request).首先,每个调用(或每个 HTTP 请求)都会实例化控制器。 You shouldn't maintain any state by introducing static properties, fields or methods inside the controllers.您不应通过在控制器中引入 static 属性、字段或方法来维护任何 state。

In your case, I would create a service class ServiceSignUp that consists of the CreateOnSignUp method.在您的情况下,我将创建一个包含CreateOnSignUp方法的服务 class ServiceSignUp

 public interface IServiceSignUp
 {
    async Task CreateOnSignUp(TicketReceivedContext ticketReceivedContext);
 }

 public class ServiceSignUp : IServiceSignUp
 {
    public async Task CreateOnSignUp(TicketReceivedContext ticketReceivedContext)
    {
       ....
    } 
 }

Then register them with the build-in IoC container like services.AddSingleton<IServiceSignUp, ServiceSignUp>();然后将它们注册到内置的 IoC 容器中,例如services.AddSingleton<IServiceSignUp, ServiceSignUp>();

In the ConfigureServices() method inside the StartUp class:StartUp class 内的ConfigureServices()方法中:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IServiceSignUp, ServiceSignUp>();

    services.AddAuthentication(options =>
                ...

            .AddOpenIdConnect("Auth0", options =>
            {
                options.Scope.Add("openid");
                ...
                options.Events = new OpenIdConnectEvents
                {
                    OnTicketReceived += ServiceSignUp.CreateOnSignUp,
                    ...
                };
            });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM