私はうまく動作するマルチテナントWeb APIを作成しました。今では、テスト用のネイティブクライアントを構築したいと考えています。 Web APIアプリは1人のテナント(webapitenant
)で定義されています。テストアプリケーションは、Web APIに管理者の同意を得た別のテナント(clienttenant
)で定義されています。他のテナントのネイティブアプリからWeb APIのアクセストークンを取得するにはどうすればよいですか?
testClientId
をWeb APIのアプリマニフェストにknownClientApplication
、oauth2AllowImplicitFlow
を有効にしました。テストクライアントには、Web APIアプリケーションに対するアクセス権が与えられています。
GetAccessToken:
var userCredential = new UserCredential("[email protected]", "password");
var context = new AuthenticationContext("https://login.windows.net/common");
return context.AcquireToken("https://webapitenant.onmicrosoft.com/webApiResourceUri", testClientId, userCredential).AccessToken;
例外がスローさ:'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
追加情報:スローAADSTS65001: The user or administrator has not consented to use the application with ID 'nativeclientid'. Send an interactive authorization request for this user and resource.
例外:'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
追加情報:AADSTS65001: The user or administrator has not consented to use the application with ID nativeclientid. Send an interactive authorization request for this user and resource.
更新 私は受け入れることができる同意書を強制的にダミーのコンソールアプリケーションを作成しました。 ADALはトークンを返しますが、Web APIはそれらを拒否します(ステータス401)。
var parameters = new PlatformParameters(PromptBehavior.Always);
var context = new AuthenticationContext("https://login.windows.net/common");
var token = context.AcquireTokenAsync
("https://webapi.onmicrosoft.com/appuri",
"testappid",
new Uri("https://webapi.azurewebsites.net"), parameters).Result.AccessToken;
Console.WriteLine(token); //Output: oauth token
var client = new HttpClient
{
BaseAddress = new Uri("https://webapi.azurewebsites.net/api/")
};
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = client.GetAsync("tenants").Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
// Output: {"$type":"System.Web.Http.HttpError, System.Web.Http","Message":"Authorization has been denied for this request."}
My appはそのように設定されています。 コンソールアプリケーションを作成してPromptBehaviour.Alwaysを使用するだけで、自分のコードを動作させることができます。アプリケーションを受け入れると、コンソールアプリと同じネイティブアプリを使用していたので、テストエクスプローラから統合テストが始まりました。 これを回避する方法はありますか?明らかに、ビジュアルスタジオテストランナーは、同意書を示すことはできません。 –
トークンがすでにWeb APIで動作していることを意味しましたか? Azure ADで保護されているWeb APIをテストするためにテストランナーを使用することに関する問題があると正しく理解していれば、新しいスレッドを再度開いて他のコミュニティがこの問題を簡単に認識して助けになることをお勧めします。 –
私は現在の調査結果とあなたのコメントに基づいて新しい質問を作成しました。ご協力ありがとうございました。 http://stackoverflow.com/questions/41146893/how-do-i-test-a-azure-ad-protected-web-api-in-with-visual-studio-test-adapter –