所有路由都使用dotnet核心中的身份验证处理程序

时间:2020-05-20 05:26:56

标签: .net-core

我一直在尝试为我的特定路由或控制器添加一种过滤器 这将验证标头的用户身份,并允许该用户访问路由 但是,即使我不使用控制器顶部的[Authorize],也会调用我的默认身份验证方案,而且我也不想调用它。

使用以下代码

 service.AddAuthentication(AuthenticationConstants.ValidateUserAuthenticationScheme)
            .AddScheme<UserAuthenticationSchemeOptions, UserAuthenticationHandler>
                   (AuthenticationConstants.ValidateUserAuthenticationScheme, op => { })
            .AddScheme<ServiceAuthenticationSchemeOptions, ServiceAuthenticationHandler>
                   (AuthenticationConstants.ValidateServiceAuthenticationScheme, op => { });

在Startup.cs上

            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

这是我不想要的路线 对于下面的控制器,我根本不想使用授权。

 [Route("api/[controller]")]
    [ApiController]
    public class HealthyController : ControllerBase
    {
        public IActionResult Get()
        {
            string status = "running";
            return Ok(status);
        }
    }

这很好用

[Authorize]
        [HttpGet]
        [Route("auth/x-user")]
        public IActionResult xuser()
        {
            return Ok("Working with x-user and x-service");
        }
    ```

0 个答案:

没有答案