使用Azure AD B2C身份验证为服务

ruf*_*fen 5 azure oauth-2.0 azure-ad-b2c

我们已经使用Azure AD B2C和OAuth设置了我们的应用程序,效果很好,但是我试图将其身份验证为服务,以便对服务调用进行服务。我对此并不陌生,但是我已经学习了有关Pluralsight的一些课程,以了解如何在“常规” Azure Active Directory上执行此操作,并且可以使其正常工作,但是在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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

服务的安全性是这样的:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

在我的B2C租户中,我有两个不同的应用程序,它们的设置大致如下:

Azure B2C应用设置 这两个应用程序都已设置了来自“密钥”选项的机密信息。与使用Azure Active Directory相比,生成的密钥的结构略有不同。

我可以成功获取令牌,但是在尝试连接到其他服务时得到401。与Azure Active Directory相比,使用B2C时,在授权方面我是否必须做些不同的事情?

Chr*_*ett 5

在以下情况下,Azure Active Directory B2C可以发出访问令牌以供Web或本机应用程序访问API应用程序:

  1. 这两个应用都已在B2C注册;和
  2. 访问令牌是作为交互式用户流(即授权码或隐式流)的结果而发出的。

当前,不支持您的特定方案(您需要发出访问令牌以供守护程序或服务器应用程序访问API应用程序(即,客户端凭据流)),但是您可以同时注册这两个方案通过B2C租户的“应用程序注册”刀片应用程序。

您可以在以下位置通过B2C升级对客户端凭据流的支持:

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

如果API应用程序要同时从Web /本地应用程序和守护程序/服务器应用程序接收令牌,则必须配置API应用程序以验证来自两个令牌发行者的令牌:一个是B2C,另一个是Azure AD B2C租户中的目录。

  • 感谢您确认。我昨晚发现了相同的信息,并使其通过B2C租户的App注册刀片工作,但是将其直接直接包含在B2C应用中会更容易。 (3认同)