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 Active Directoryを使用する場合とは少し異なります。
トークンを正常に取得できますが、他のサービスに接続しようとすると401が表示されます。 Azure Active Directoryと比較してB2Cを使用する場合、認可側で何か別の処理を行う必要がありますか? APIアプリにウェブまたはネイティブアプリによるアクセスのためのアクセストークンを発行することができ
[ヘッドレス認証Azure AD b2c](https://stackoverflow.com/questions/35072371/headless-authentication-azure-ad-b2c)の可能な複製 – spottedmahn