首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OAuth对客户端进行身份验证后,如何调用经过身份验证的ServiceStack服务?

使用OAuth对客户端进行身份验证后,如何调用经过身份验证的ServiceStack服务?
EN

Stack Overflow用户
提问于 2013-09-22 05:52:19
回答 1查看 935关注 0票数 2

假设我有一个web客户端(即MVC4客户端),它使用oAuth提供商(即Facebook、谷歌等)对用户进行身份验证。我想在我的客户端逻辑中调用另一个web服务,该web服务也通过oAuth提供者进行身份验证。

来自客户端的web服务请求是什么样子的?我需要向web服务传递什么?

EN

回答 1

Stack Overflow用户

发布于 2013-09-23 23:33:41

我建议你复习一下这个问题,How do I authorize access to ServiceStack resources using OAuth2 access tokens via DotNetOpenAuth?。这张海报提供了他的最终解决方案,包括一个样本解决方案的链接,他已经优雅地开源。他的解决方案的客户端代码如下所示:

代码语言:javascript
复制
// 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进行请求签名

代码语言:javascript
复制
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提供商的要求,即签名、令牌等。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18938054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档