Click here to Skip to main content
15,896,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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


C#
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
Posted

1 solution

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??
 
Share this answer
 
Comments
miss786 18-Feb-14 10:35am    
Hi, Thank you for your reply. I have tried removing the 'override' but then I get another error - 'object' does not contain a definition for 'SendAsync' -- error. On the following line:
"return base.SendAsync(request, cancellationToken);"

I am little confuse by, what you mean by "base class", if you don't mind clarifying this.

for example(do you mean something like this):
public User(cdwEntities context) : base(context) {
// ...
}

Thank you for your help.
Vedat Ozan Oner 18-Feb-14 10:45am    
It is the basic concept of OOD (object oriented design). see this article: http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles
Vedat Ozan Oner 18-Feb-14 10:46am    
you should do something like
public class BasicAuthHandler: BASE_AUTH_HANDLER {
...
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900