WCF how to pass token for authentication?

本秂侑毒 提交于 2019-11-28 22:55:40

问题


I have a WCF service which would like to support basicHttpBinding and webHttpBinding. When the client successfully login, server will generate a token for client to pass to server on all the request make later. Question is how the client can pass the token to server? I don't want to add an extra parameter on every web method to hold the token.


回答1:


Typically, the best way to do something like this is passing such "meta-information" in a WCF header. You can easily create a message inspector to extend WCF (it's really not that scary and hard to do!) which would inject the token into every outgoing request from the client, and retrieve it from the header and validate it on the server side.

There are a number of pretty good blog post out there showing you how to create a message inspector:

  • Richard Hallgren's WCF postings
  • Writing a WCF message inspector
  • Automatic Culture Flowing with WCF by using Custom Behaviour

Check out the two relevant interfaces to implement:

  • IClientMessageInspector on the client side, which has a BeforeSendRequest and AfterReceiveReply message to implement
  • IDispatchMessageInspector on the server side, which has a AfterReceiveRequest and BeforeSendReply method to implement


来源:https://stackoverflow.com/questions/2605140/wcf-how-to-pass-token-for-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!