没有HTTP上下文的ASP网络核心身份验证。 (智威汤逊)

时间:2020-05-30 15:29:46

标签: asp.net-core authentication jwt authorization decorator

我对.net核心中的auth有疑问。我需要你的建议。我有可以从Web api控制器调用的处理程序(命令或查询),也可以从Kafka使用者调用的处理程序。

由于可以从Web API控制器或Kafka使用者调用处理程序,因此我想集中auth逻辑作为处理程序的装饰器。

示例。

public class CreateUserCommand : ICommand
{
   public string Email {get; set;}
   public string Password {get; set;}
}

public class CreateUserCommandHandler : ICommandHandler< CreateUserCommand >
{
    public void Handle(CreateUserCommand command)
    {
      // some business logic
    }
}

public class AuthDecoator : ICommandHandler<TCommand>
{
   public AuthDecoator (ICommandHandler<TCommand> decoratee)

    public void Handle(CreateUserCommand command)
    {
      // handle auth, if valid 
      decoratee.Handle(command);
    }
}

// From Web API
public class UsersController : ControllerBase
{
   public IActioneResult Register(string email, string password)
   {
       createUserCommandHandler.Handler(new CreateUserCommand { Email = email, 
         Password = password })
   }
}

public class RegisterUser
{
   public string Email {get; set;}
   public string Password {get; set;}
}

// From Consumer
public class RegisterConsumer : ConsumerBase
{
   public ConsumeResult Consume(RegisterUser)
   {
       createUserCommandHandler.Handler(new CreateUserCommand { Email = email, 
         Password = password })
   }
}

通过这种方式,我将有机会保护所有类型的呼叫,例如Kafka消费,http或批处理作业

所以我的问题是

  1. 让认证作为处理程序的装饰者好吗?
  2. 如果是的话,如何在没有http上下文的情况下编写自己的“ AuthenticationHandler”?我的功能代码不应该知道运输协议,例如http / s

我检查了诸如“ AuthenticationHandler”之类的选项,但都取决于http上下文。

0 个答案:

没有答案