APIアプリケーションにアクセスしようとしています。私はAzureでホストし、Azure ADで保護しています。Azure ADベアラートークンを使用してAPIアプリケーションに対してWebアプリケーションを認証します。
APIアプリケーションでは、App Service Authentication = Azure Active Directoryの「Express」管理モードを設定しました。
「古典的な」ポータルでは、ADの下に2つのアプリケーションを作成しました。 1つはAPI App用で、もう1つはWeb App用です。また、Web Appについては、API Appの「他のアプリケーションへのアクセス許可」の下にエントリを追加しました(ただし、「Appへのアクセスに必要なユーザー割り当て」がAPIアプリケーションではオフです)。また、Web Appのキーも生成しました。
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"];
static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
private static string ApiId = ConfigurationManager.AppSettings["ApiId"];
private static AuthenticationContext authContext = new AuthenticationContext(authority);
private static ClientCredential clientCredential = new ClientCredential(clientId, appKey);
...
AuthenticationResult result = null;
int retryCount = 0;
bool retry = false;
do
{
retry = false;
try
{
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
result = await authContext.AcquireTokenAsync(ApiId, clientCredential);
}
catch (AdalException ex)
{
if (ex.ErrorCode == "temporarily_unavailable")
{
retry = true;
retryCount++;
Thread.Sleep(3000);
}
}
} while ((retry == true) && (retryCount < 3));
if (result == null)
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Could not authenticate against API.");
しかし: -
ここに挙げた例のコードに続きhttps://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity ...
私は正常に次のコードを使用してベアラトークンを取得することができます私はベアラトークンをWeb AppからAPI Appへのリクエストと共に使用します。私はいつも401の不正な応答を受け取ります:
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Wed, 13 Jul 2016 08:43:09 GMT
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="MY-API-APP-ID-IS-HERE"
X-Powered-By: ASP.NET
Content-Length: 58
Content-Type: text/html
}
これは私が401で失敗の要求を行うために使用しているコードです:
var apiUri = new Uri(ConfigurationManager.AppSettings["ApiUrl"] + "/api/MethodImCalling");
var client = new RestClient(apiUri.GetLeftPart(UriPartial.Authority));
var request = new RestRequest(apiUri, Method.GET);
request.AddHeader("Authorization", "Bearer " + result.AccessToken);
request.AddParameter("something", somevalue);
var response = client.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
return Request.CreateResponse(response.StatusCode); // Relay non-successful response
私が間違っている可能性がありますまたは行方不明です何任意のアイデア?前もって感謝します!
Logic Appを既にAzureに持っていて問題なくAPIアプリケーションにアクセスしていますが、ロジックアプリjsonの認証資格情報には「オーディエンス」パラメータが含まれています。上記のコードは "聴衆"を使用しないので、これはパズルの欠落部分である可能性があります。もしそうなら、どうすれば追加できますか? Webアプリケーションは、APIアプリケーションにアクセスするように構成されているかを示す
また、あなたは承認のためのあなたのAPIを設定した方法の詳細を共有することができます:
あなたはここでこれらの2つの異なるアプローチについての詳細な情報を見つけることができますか? –
@PhilippeSignoret - 私はAppサービスを使用しています。Authenication = Azure Active Directory "Express"管理モード(質問にも追加されます)。これはあなたが後にしているディテールですか?他にどのような詳細を提供するかわからない。乾杯。 – Gavin
Azureにデプロイする前にサンプルコードがローカルで正常に動作していますか?アクセストークンでAPIにアクセスできない場合は、JWTデコードされたツールを使用してトークンを分析することをお勧めします。ここでは、JWTデコードされたWebページhttps://jwt.io/があります。 –