如何编写自定义消息处理程序? [英] How to write write a custom message handler?

查看:91
本文介绍了如何编写自定义消息处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个自定义消息处理程序,以便对我的api进行身份验证。我目前坚持以下错误 - BasicAuthHandler.SendAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)':找不到合适的方法来覆盖 - 错误。



I am trying to write a write a custom message handler, to authencicate my api. I currently stuck on the following error -- BasicAuthHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)': no suitable method found to override -- error.

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)







public class BasicAuthHandler
    {
        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic";

        private readonly iUser Repository = new User();

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            AuthenticationHeaderValue authValue = request.Headers.Authorization;


            if (authValue == null || authValue.Scheme != BasicAuthResponseHeaderValue)
            {
                return Unauthorized(request);
            }
            string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authValue.Parameter)).Split(new[] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[1]))
            {
                return Unauthorized(request);
            }
            api_login user = Repository.Validate2(credentials[0], credentials[1]);
            if (user == null)
            {
                return Unauthorized(request);
            }
            IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.username, BasicAuthResponseHeaderValue), null);
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;

            return base.SendAsync(request, cancellationToken);
        }





任何有关此代码可能出错的指导。非常感谢



Any guidance into where I may be going wrong with this code. Many thanks

推荐答案

我可以帮助解决这个错误。由于' BasicAuthHandler '本身是基类,因此无法覆盖任何方法。如果在方法定义中删除 override 关键字,则将清除错误。您可能忘记为 BasicAuthHandler指定基类 ??
I can help with the error. Since 'BasicAuthHandler' itself is a base class, there can't be any method to override. If you delete 'override' keyword in the method definition, the error will be cleared. Might have you forgetten to specify a base class for BasicAuthHandler??


这篇关于如何编写自定义消息处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆