假设我有一个web客户端(即MVC4客户端),它使用oAuth提供商(即Facebook、谷歌等)对用户进行身份验证。我想在我的客户端逻辑中调用另一个web服务,该web服务也通过oAuth提供者进行身份验证。
来自客户端的web服务请求是什么样子的?我需要向web服务传递什么?
发布于 2013-09-23 23:33:41
我建议你复习一下这个问题,How do I authorize access to ServiceStack resources using OAuth2 access tokens via DotNetOpenAuth?。这张海报提供了他的最终解决方案,包括一个样本解决方案的链接,他已经优雅地开源。他的解决方案的客户端代码如下所示:
// Create the ServiceStack API client and the request DTO
var apiClient = new JsonServiceClient("http://api.mysite.com/");
var apiRequestDto = new Shortlists { Name = "dylan" };
// Wire up the ServiceStack client filter so that DotNetOpenAuth can
// add the authorization header before the request is sent
// to the API server
apiClient.LocalHttpWebRequestFilter = request => {
// This is the magic line that makes all the client-side magic work :)
ClientBase.AuthorizeRequest(request, accessTokenTextBox.Text);
}
// Send the API request and dump the response to our output TextBox
var helloResponseDto = apiClient.Get(apiRequestDto);
Console.WriteLine(helloResponseDto.Result);这里提供了一个类似的解决方案:https://stackoverflow.com/a/13791078/149060,它演示了按照OAuth 1.0a进行请求签名
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<MySecuredResponse> ("/my/service");当然,您需要进行调整以适应您的OAuth提供商的要求,即签名、令牌等。
https://stackoverflow.com/questions/18938054
复制相似问题