2017-10-31 9 views
2

Azure AD B2CとOAuthを使用してアプリケーションをセットアップしましたが、これはうまく機能しますが、サービスコールとしてサービスを認証するために認証しようとしています。私はこれに少し新しくなっていますが、Pluralsightのいくつかのコースに続いて、 "普通の" Azure Active Directoryでこれを実行する方法について説明しました。私はそれを動作させることができますが、B2Cと同じ原則に従って動作しません。Azure AD B2Cでサービスとして認証する

私はこのクイックコンソールアプリケーションがあります。

class Program 
{ 
    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; //APIClient ApplicationId 
    private static string appKey = ConfigurationManager.AppSettings["ida:appKey"]; //APIClient Secret 
    private static string aadInstance = ConfigurationManager.AppSettings["ida:aadInstance"]; //https://login.microsoftonline.com/{0} 
    private static string tenant = ConfigurationManager.AppSettings["ida:tenant"]; //B2C Tenant 
    private static string serviceResourceId = ConfigurationManager.AppSettings["ida:serviceResourceID"]; //APP Id URI For API 
    private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 

    private static AuthenticationContext authContext = new AuthenticationContext(authority); 
    private static ClientCredential clientCredential = new ClientCredential(clientId, appKey); 

    static void Main(string[] args) 
    { 
     AuthenticationResult result = authContext.AcquireToken(serviceResourceId, clientCredential); 
     Console.WriteLine("Authenticated succesfully.. making HTTPS call.."); 

     string serviceBaseAddress = "https://localhost:44300/"; 
     HttpClient httpClient = new HttpClient(); 
     httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
     HttpResponseMessage response = httpClient.GetAsync(serviceBaseAddress + "api/location?cityName=dc").Result; 

     if (response.IsSuccessStatusCode) 
     { 
      string r = response.Content.ReadAsStringAsync().Result; 
      Console.WriteLine(r); 
     } 
    } 
} 

をし、サービスは、このように確保されている:私のB2Cのテナントで

private void ConfigureAuth(IAppBuilder app) 
    { 
     var azureADBearerAuthOptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
     { 
      Tenant = ConfigurationManager.AppSettings["ida:Tenant"], 
      TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() 
      { 
       ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] 
      } 
     }; 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(azureADBearerAuthOptions); 
    } 

私は、このようにかなりのセットアップされている2つの異なるアプリケーションを持っています:

Azure B2C app setup "キー"オプションからの秘密が両方のアプリケーションに設定されています。生成されるキーは、Azure Active Directoryを使用する場合とは少し異なります。

トークンを正常に取得できますが、他のサービスに接続しようとすると401が表示されます。 Azure Active Directoryと比較してB2Cを使用する場合、認可側で何か別の処理を行う必要がありますか? APIアプリにウェブまたはネイティブアプリによるアクセスのためのアクセストークンを発行することができ

+0

[ヘッドレス認証Azure AD b2c](https://stackoverflow.com/questions/35072371/headless-authentication-azure-ad-b2c)の可能な複製 – spottedmahn

答えて

3

のAzure Active DirectoryのB2Cの場合:

  1. B2Cに登録されているこれらのアプリケーションの両方;
  2. アクセストークンは、対話型ユーザーフロー(つまり、認証コードまたは暗黙的なフロー)の結果として発行されます。

現在、特定のシナリオ(デーモンやサーバーアプリケーションからAPIアプリへのアクセスのためにアクセストークンを発行する必要がある場合)(つまり、クライアントの資格情報フロー)はサポートされていませんB2Cテナントの「App Registrations」ブレードを介して、これらのアプリケーションの両方を登録することができます。 APIアプリは、ウェブ/ネイティブアプリだけでなく、デーモン/サーバーアプリケーションの両方からのトークンを受信する場合は、

https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/18529918-aadb2c-support-oauth-2-0-client-credential-flow

あなたは、クライアントの資格情報のサポートをupvoteすることができますがでB2Cによって流れ2つのトークン発行者からのトークンを検証するようにAPIアプリケーションを構成する必要があります.1つはB2C、もう1つはB2CテナントのAzure ADディレクトリです。

+2

ありがとうございます。私は昨夜同じ情報を見つけて、B2Cテナント用のApp登録ブレードを使って動作させましたが、B2Cアプリケーションに直接入れるのは簡単でした。 – ruffen

+0

@ruffen - これをどうやって手がかりをつけるか? – ubienewbie

+0

@ubienewbie [こちらの記事を参照](https://stackoverflow.com/a/49036907/185123) – spottedmahn

関連する問題