是否可以覆盖HostAuthenticationFilter过滤器?

时间:2018-03-09 22:22:15

标签: c# asp.net-web-api owin bearer-token authorize-attribute

是否可以覆盖HostAuthenticationFilter过滤器以自定义响应消息?

这个配置如下: 它是持票人令牌授权。

config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

2 个答案:

答案 0 :(得分:1)

你可以这样覆盖:

public class HostAuthenticationFilterCustom : HostAuthenticationFilter
    {
        public HostAuthenticationFilterCustom(string authenticationType) : base(authenticationType)
        {
        }
        public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            return base.AuthenticateAsync(context,cancellationToken);
        }
        public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
        {
            return base.ChallengeAsync(context,cancellationToken);
        }
    }

答案 1 :(得分:0)

您需要将IAuthenticationFilter添加到基类,因为HostAuthenticationFilter在AuthenticateAsync函数上未设置覆盖重写器。另外,您需要在AuthenticateAsync和其他派生函数上添加“ new”修饰符,以免从编译器中收到在基类中定义相同函数而没有覆盖的错误。

public class HostAuthenticationFilterCustom : HostAuthenticationFilter, IAuthenticationFilter
    {
        public HostAuthenticationFilterCustom(string authenticationType) : base(authenticationType)
        {
        }
        public new Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            return base.AuthenticateAsync(context,cancellationToken);
        }
        public new Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
        {
            return base.ChallengeAsync(context,cancellationToken);
        }
    }