小编Joh*_*rre的帖子

如何为 ActiveAuthenticationSchemes 设置默认值?

在我的 ASP.Net Core 2 项目中,我有一个AuthenticationHandler想要插入的自定义中间件。

public class BasicAuthenticationMiddleware : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public BasicAuthenticationMiddleware(IOptionsMonitor<AuthenticationSchemeOptions> options,
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
        : base(options, logger, encoder, clock)
    {
    }
    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        var principal = new GenericPrincipal(new GenericIdentity("User"), null);
        var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), "BasicAuth");
        return Task.FromResult(AuthenticateResult.Success(ticket));
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的启动中,我有以下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = "BasicAuth";
        options.DefaultChallengeScheme = "BasicAuth";
        options.AddScheme("BasicAuth", x => {
            x.DisplayName = "BasicAuthenticationMiddleware";
            x.HandlerType = typeof(BasicAuthenticationMiddleware);
        }); …
Run Code Online (Sandbox Code Playgroud)

c# .net-core asp.net-core-2.0

5
推荐指数
2
解决办法
937
查看次数

标签 统计

.net-core ×1

asp.net-core-2.0 ×1

c# ×1