在ASP.NET Core 2.1 MVC中使用多种身份验证方案

时间:2019-12-07 03:59:39

标签: authentication asp.net-core-mvc

我的cookie身份验证工作正常,但我想添加基本身份验证以用于Web API。我从其他地方开始遵循这些步骤来实现基本身份验证处理程序,但它似乎从未达到HandleAuthenticateAsync()

这是在startup()中设置的方式:

public void ConfigureServices(IServiceCollection services)
{
    services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = "/Users/LogIn";
                options.LogoutPath = "/Users/Logout";
                options.AccessDeniedPath = "/Users/AccessDenied";
            })
            .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
}

这是控制器:

[Route("api/[controller]")]
[ApiController]
[Authorize(AuthenticationSchemes = "BasicAuthentication")]
public class EmployeesController : ControllerBase
{
   .....
}

0 个答案:

没有答案