Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headers in OperationContext

Tags:

c#

wcf

I'd make a little project (WCF + REST) and I have a small problem. I want make my Authorization and Authentication class.

My Authorization class:

//validate api key
public class BasicAuthorization : ServiceAuthorizationManager
{
    public override bool CheckAccess(OperationContext operationContext, 
        ref Message message)
    {
        //some code
    }
}

My Authenticate class

// validation user login & password
public class BasicAuthentication : ServiceAuthenticationManager
{
    public override ReadOnlyCollection<IAuthorizationPolicy> Authenticate(
        ReadOnlyCollection<IAuthorizationPolicy> authPolicy, Uri listenUri, 
        ref Message message)
    {
        //some code
    }
}

I have too some config file

<behavior>
  <serviceAuthorization 
      serviceAuthorizationManagerType="WCF.BasicAuthorization, WCF"/>
  <serviceAuthenticationManager 
      serviceAuthenticationManagerType="WCF.BasicAuthentication, WCF"/>
</behavior>

The code in class is unimportant - is not a problem.

My problem is how to get Headers from operationContext or message class. How i say before, i make this in rest, so i want manual set Authorizaion header / www-authenticate header, but application doesn't see it.

I turn on the Fiddler2, and try put any header for example :

Content-Type: application/xml
Authorization: Basic bla23rwerfsd3==
User-Agent: Fiddler
Host: localhost:59305

And the message.Headers / operationContext.Headers doesn't has any my header (has only other one), no Authorization, no Content-Type

like image 506
user634199 Avatar asked Feb 25 '11 13:02

user634199


1 Answers

You can access the headers during your web-operation using the System.ServiceModel.Web.WebOperationContext class, which has a static property "Current", which represents the current-context. It provides an "IncomingRequest" property that contains a "Header" property of type "WebHeaderCollection".

like image 131
J. Tihon Avatar answered Oct 18 '22 22:10

J. Tihon