【问题标题】:Asp.net Core 3.1 MVC - Disable Authentication in Development EnvironmentAsp.net Core 3.1 MVC - 在开发环境中禁用身份验证
【发布时间】:2021-12-10 21:54:49
【问题描述】:

我们已将代码与 Azure AD 集成以进行身份​​验证。根据我们的 Azure AD 策略,我们甚至需要通过 MFA 进行身份验证。因此,在开发过程中,我们需要输入密码和 MFA,这非常令人沮丧。

是否可以在不删除 [Authorize] 标签的情况下禁用开发中的身份验证或者我们可以添加虚拟主体对象吗?

这是我们在 Startup 类的 ConfigureServices 中代码的屏幕截图

我看过几个帖子,但没有一个选项适合我们。

请帮助我们。提前致谢

【问题讨论】:

    标签: c# asp.net-core azure-active-directory


    【解决方案1】:

    .net core 3.1中,如果环境类型为Development,可以添加自定义IAuthorizationHander有条件地绕过auth。

    answer 中的一个示例:

    /// <summary>
    /// This authorisation handler will bypass all requirements
    /// </summary>
    public class AllowAnonymous : IAuthorizationHandler
    {
        public Task HandleAsync(AuthorizationHandlerContext context)
        {
            foreach (IAuthorizationRequirement requirement in context.PendingRequirements.ToList())
                context.Succeed(requirement); //Simply pass all requirements
    
            return Task.CompletedTask;
        }
    }
    

    然后在 Startup.ConfigureServices 中有条件地注册这个处理程序。

    private readonly IWebHostEnvironment _env;
    public Startup(IWebHostEnvironment env)
    {
        _env = env;
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
      {...}
    
      //Allows auth to be bypassed
      if (_env.IsDevelopment())
        services.AddSingleton<IAuthorizationHandler, AllowAnonymous>();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 2020-12-28
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多