【问题标题】:Is it possible to override HostAuthenticationFilter filter?是否可以覆盖 HostAuthenticationFilter 过滤器?
【发布时间】:2018-03-09 22:22:14
【问题描述】:

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

配置如下: 它是一个不记名令牌授权。

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

【问题讨论】:

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


    【解决方案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);
            }
        }
    

    【讨论】:

    • 这里如何修改响应对象?
    • @user3711357 我不知道你想要什么,我展示了可以覆盖过滤器,如何使用它取决于你,不知道你的响应对象是什么意思,你的目标,也许你的问题定义错误。
    • 如果在HostAuthenticationFilterCustom 中找到401响应,那么如何修改响应以及如何检查响应是否为401或任何其他代码。
    • 它没有调用,全局注册过滤器。
    • 它没有调用HostAuthenticationFilterCustom的任何方法,全局注册过滤器。看起来,它没有被覆盖。
    【解决方案2】:

    您需要将 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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多