2

oAuth プロバイダー (Facebook、Google など) を使用してユーザーを認証する Web クライアント (MVC 4 クライアント) があるとします。クライアント ロジックで別の Web サービスを呼び出したいのですが、その Web サービスも oAuth プロバイダーで認証されます。

クライアントからの Web サービス要求はどのように見えるでしょうか? Web サービスに何を渡す必要がありますか?

4

1 に答える 1

2

DotNetOpenAuth 経由で OAuth2 アクセス トークンを使用して ServiceStack リソースへのアクセスを承認するにはどうすればよいですか?という質問を確認することをお勧めします。. 投稿者は、サンプル ソリューションへのリンクを含む最終的なソリューションを提供してくれました。彼のソリューションのクライアント側コードは次のようになります。

// 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 プロバイダーの要件 (署名、トークンなど) に合わせて調整する必要があります。

于 2013-09-23T15:33:40.653 に答える