The ServiceStack docs are full of examples on how to use server side implementation of authentication of a user. But how does one set the user credentials on the client
Answering myself, as I've found a nice way to do it using the LocalHttpWebRequestFilter
hook in the JsonServiceClient
:
For securing a web service with OAuth 1.0a, every http request has to send a special Authorization: header. Within this header field, a hash (signature) must be send that uses some characteristics of the request as input data, like the hostname, request url and others.
Now it seems the LocalHttpWebRequestFilter
is called by ServiceStack right before the http request is made, and exposes the underlying HttpWebRequest
object, where one can add extra headers and access the required fields of the request.
So my solution is now basically:
var client = new JsonServiceClient (baseUri);
client.LocalHttpWebRequestFilter += (request) => {
// compute signature using request and a previously obtained
// access token
string authorization_header = CalculateSignature (request, access_token);
request.Headers.Add ("Authorization", authorization_header);
};
var response = client.Get ("/my/service");
Note that I use the Devdefined.OAuth library to do all the heavy stuff in CalculateSignature()
. The creation of request token, obtaining user authorization, and exchanging the request token for access token as required by OAuth is done outside of ServiceStack, before the above service calls.