开发者

How can I add a header/datamember to all DataContracts in a WCF service?

I am looking to extend existing services and add Authorization to every call being made. The problem I have is that I don't know how to do this in the best possible manner. What I need to do is to send the name of the module calling the WCF service. I already send the username and password for the service and now I need to extend that with the name of the module calling the service. This is because we might allow a user to open a module and display data from another module but not from a third.

If we would have used message contracts I woul开发者_如何转开发d just add a MessageHeader for this and set that header when I create the request. That is unfortunately not an option with DataContracts so I was considering the following two alternatives.

Adding a DataMember in a base class with Order=1000 or something like that. I don't know what will happen if we add another DataMember before that though?

Create the property for the module name and set a header in the transport instead. Not really fond of this one though. It's pretty abstract and hard to follow.

Which one is the least evil or do you have a better suggestion?

EDIT 1: The problem is not how to send a header to the service the problem is how to send a header with a specific value to the server. In the message inspector I can only create generic instances with message.GetBody<DataContract>(); this means I have to know the type which I don't know how to.

EDIT 2: The issue here is that in our application we want to restrict access to a call based on from where the call is made so I need to pass this information. Let's say I make the call to MyService from FindUserModule then I need to add the id of that module in a header so that the AuthorizationManager can check if that user really should be authorized. This is due to service calls being used from many modules.


Handle this as SOAP header in custom message inspector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜