gpt4 book ai didi

azure - 使用 Azure AD B2C 作为服务进行身份验证

转载 作者:行者123 更新时间:2023-12-04 09:42:11 25 4
gpt4 key购买 nike

我们已经使用 Azure AD B2C 和 OAuth 设置了我们的应用程序,这工作正常,但是我正在尝试作为服务进行身份验证,以便进行服务到服务的调用。我对此有点陌生,但我已经遵循了 Pluralsight 上的一些类(class),了解如何在“普通”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);
}
}
}

服务的安全性如下:

    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);
}

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

Azure B2C app setup这两个应用程序都设置了来自“ key ”选项的 secret 。生成的 key 的结构与使用 Azure Active Directory 时略有不同。

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

最佳答案

在以下情况下,Azure Active Directory B2C 可以颁发访问 token ,以便 Web 或 native 应用程序访问 API 应用程序:

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

目前,您的特定场景(您需要颁发访问 token 以便守护程序或服务器应用程序访问 API 应用程序(即客户端凭据流))不受支持,但您可以注册这两个应用程序都通过 B2C 租户的“应用程序注册” Blade 进行操作。

您可以通过以下方式投票支持 B2C 的客户端凭据流程:

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

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

关于azure - 使用 Azure AD B2C 作为服务进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47035605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com