Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:38:22+00:00 2026-05-28T23:38:22+00:00

I’m currently developing an WCF4 based REST webservice. Most calls don’t need authorization, but

  • 0

I’m currently developing an WCF4 based REST webservice. Most calls don’t need authorization, but some do, so my plan is to implement an RequireAuthorization attribute that handles API key authorization for some calls.

After reading Implementing an Authorization Attribute for WCF Web API I implemented an HttpOperationHandler that handles the API key validation in the OnHandle method and added it to the RequestHandlers in the service configuration.

Basically this works fine, but I need to pass the user info I extracted in the AuthOperationHandler to the service, but I can’t find any information on this.

Can anyone help?

public class ServiceConfiguration : WebApiConfiguration
{
    public ServiceConfiguration()
    {
        RequestHandlers = (c, e, od) => {
            var authorizeAttribute = od.Attributes.OfType<RequireAuthorizationAttribute>().FirstOrDefault();
            if (authorizeAttribute != null)
            {
                c.Add(new AuthOperationHandler(authorizeAttribute));
            }
        };

        CreateInstance = (serviceType, context, request) => RepositoryFactory.GetInstance(serviceType);
    }
}

public class AuthOperationHandler : HttpOperationHandler<HttpRequestMessage, HttpRequestMessage>
{
    protected override HttpRequestMessage OnHandle(HttpRequestMessage input)
    {
        // API Key validation here
        var user = RetrieveUserByApiKey(input);

        return input;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:38:23+00:00Added an answer on May 28, 2026 at 11:38 pm

    I found a solution to this, but it does come with a caveat.

    The way I did it was for my operation handler to return some form of custom authentication state class instead of a HttpResponsemessage – in my case I used an AuthenticationTicket.

    public class AuthOperationHandler : HttpOperationHandler<HttpRequestMessage, AuthenticationTicket>
    {
        protected override AuthenticationTicket OnHandle(HttpRequestMessage input)
        {
            var ticket = RetrieveAuthenticationTicket(input);
    
            return ticket;
        }
    }
    

    And the AuthenticationTicket class just stores the account ID.

        public class AuthenticationTicket
        {
            public AuthenticationTicket(int accountId)
            {
                AccountId = accountId;
            }
    
            public int AccountId { get; set; }
        }
    

    Then in your service method you add an AuthenticationTicket as a parameter and this will be populated for you by the operation handler.

        [OperationContract]
        [WebInvoke(UriTemplate = "people", Method = "GET")]
        HttpResponseMessage<IList<Person>> LoadPeople(AuthenticationTicket ticket);
    

    You can then use the ticket in your method implementation.

    However, this acts a bit funny with POST, PUT and DELETE operations if you need to use an additional parameter, like so:

        [OperationContract]
        [WebInvoke(UriTemplate = "people", Method = "PUT")]
        HttpResponseMessage AddPerson(AuthenticationTicket ticket, Person person);
    

    You’ll get an exception along the following lines:

    The HttpOperationHandlerFactory is unable to determine the input
    parameter that should be associated with the request message content
    for service operation ‘AddPerson’. If the operation does not expect
    content in the request message use the HTTP GET method with the
    operation. Otherwise, ensure that one input parameter either has it’s
    IsContentParameter property set to ‘True’ or is a type that is
    assignable to one of the following: HttpContent, ObjectContent1,
    HttpRequestMessage or HttpRequestMessage
    1.

    The solution I found (not sure whether it’s right) is to wrap the resource in a HttpRequestMessage like so:

        [OperationContract]
        [WebInvoke(UriTemplate = "people", Method = "PUT")]
        HttpResponseMessage UpdatePerson(AuthenticationTicket ticket, HttpRequestMessage<Person> request);
    

    Then in your method use the following to retrieve the Person:

    var person = request.Content.ReadAsAsync<Person>().Result;
    

    Hope this helps, it works for me. But I’m still learning myself to be honest, so there may be better ways of doing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.