独自のOAuth2実装を使用するRESTサーバーと通信しようとしています。 このサーバーはJavaの別の会社によって書かれているので、私はそれにあまり影響しません。 アクセストークンURL、リフレッシュURL、クライアントID、クライアントシークレットなどの必要な情報がすべてあります。アクセストークンをリクエストしてから、このサーバーからRESTクライアントのPostmanを使用して他のデータをリクエストできます。カスタムServiceStack OAuth2プロバイダ
ここでは、C#.NET 4.6.2でこのサーバーと通信するためにServiceStackクライアント(バージョン4.5.14)を使用したいと思います。
私の問題は次のとおりです。 http://docs.servicestack.net/authentication-and-authorization#custom-authentication-and-authorizationは、サーバー側またはFacebookやGoogleに対する認証に関するものです。
私はすでに など、アクセストークンのURL、ConsumerSecretを設定し、CustomOAuth2Provider
自分自身を実装しかし、どのように、私は特定の要求を実行する前に、このプロバイダを使用するために、JsonServiceClient
を教えていますか?私はドキュメントとServiceStackのソースコードの多くを読んで、私は私の主な問題は以下の通りだと思い
:
- 私はServiceStackクライアントを乱用
は
ダニエル
、[編集]を をありがとう私が変更できない非ServiceStackアプリケーションと通信することができます。 - サードパーティアプリケーションのOAuth2実装は、同じリクエスト内で承認とトークン要求を期待しているため、100%正確ではない場合があります。
しかし、私は今すぐ動作しており、私の解決策をここに示したいと思います。それでもなお改善することができます。今すぐ受信したリフレッシュトークンを使用しません。
public class ThirdPartyAuthenticator : IDisposable
{
// TODO: Move to config
public const string AccessTokenUrl = "";
public const string ConsumerKey = "";
public const string ConsumerSecret = "";
public const string Username = "";
public const string Password = "";
/// <summary>
/// Remember the last response, instance comprehensive so we do not need a new token for every request
/// </summary>
public static ServiceModel.ThirdPartyOAuth2Response LastOAuthResponse = null;
/// <summary>
/// This already authenticated client can be used for the data requests.
/// </summary>
public JsonServiceClient AuthenticatedServiceClient { get; set; }
public ThirdPartyAuthenticator()
{
if (LastOAuthResponse == null || (LastOAuthResponse.ExpiryDateTime < DateTime.Now)) // TODO: Use Refresh token?
{
// Get token first
JsonServiceClient authClient = new JsonServiceClient(AccessTokenUrl);
authClient.UserName = ConsumerKey;
authClient.Password = ConsumerSecret;
authClient.AlwaysSendBasicAuthHeader = true;
var request = new ServiceModel.ThirdPartyOAuth2Request();
request.Username = Username;
request.Password = Password;
// Use the Get URI, because server expects username + password as query parameter
LastOAuthResponse = authClient.Post<ServiceModel.ThirdPartyOAuth2Response>(request.ToGetUrl(), request);
}
// If no exception was thrown, we have a valid token here.
AuthenticatedServiceClient = new JsonServiceClient(AccessTokenUrl);
AuthenticatedServiceClient.BearerToken = LastOAuthResponse.AccessToken;
}
public void Dispose()
{
AuthenticatedServiceClient?.Dispose();
}
}
用法:
using (var foo = new ThirdPartyAuthenticator())
{
var response = foo.AuthenticatedServiceClient.Get(new ServiceModel.GetMyData() { SomeId = 10 });
}
あなたの答えをありがとう、私はまだそれを得ることはありません。私は今すぐServiceStackホストを持っていません - サードパーティ製のサーバーアプリケーションです。 クライアントは、データ要求自体を送信する前に、アクセストークンを取得するために、このサーバーにOAuth2要求を送信する必要があります。 クライアントのラッパークラスを作成することで、リクエスト自体の前に認証リクエストを呼び出しています。 TechStacksの例の 'ServiceStackAuthenticator'クラスに似ています。 @DanielP。 –
'ServiceStackAuthenticator'クラスは、Web UIの読み込みに関わる[Xamarin.Auth](https://github.com/xamarin/Xamarin.Auth#xamarinauth)をラップし、ユーザーの同意を得て、oauthリダイレクトURLにリダイレクトしますServiceStackAuthenticator.Completedコールバックを呼び出して、サービスクライアントがブラウザ上で認証されたセッションクッキーを抽出し、サービスクライアントにそれらを設定できるようにします。 @DanielP。 – mythz
OAuth2トークンの取得を管理している場合は、[Google OAuth2の例](https://github.com/ServiceStackApps/AndroidJavaChat#login-with-in)で見られるように、Authenticate.AccessTokenプロパティに設定することで認証に使用できますgoogle-signin-button)をクリックします。これは、カスタムAuthProviderでも実装する必要があります。詳しくは、[この機能のリリースノート](http://docs.servicestack.net/releases/v4.5.8#authentication-via-oauth-accesstokens)を参照してください。 – mythz