Azure active directory dotnet核心WebApp和多个web api';s使用AzureB2C、MSAL访问令牌
我已经为WebApp和Api设置了身份验证/授权,并且工作正常。问题是当我必须引入额外的Api时,这些Api将从WebAPP调用 限制是您不能在一次调用中请求作用域混合Web API的令牌。这是服务(AAD)的限制,而不是图书馆的限制。 您必须为https://{tenant}.onmicrosoft.com/api1/read请求一个令牌 然后您可以为https://{tenant}.onmicrosoft.com/api2/read静默地获取令牌,因为这是两个不同的API。 我从和中学到了更多 因为除了几行代码之外,没有完整的示例,所以我试图找到实现此解决方案的最佳方法 目前,我已在启动中设置身份验证Azure active directory dotnet核心WebApp和多个web api';s使用AzureB2C、MSAL访问令牌,azure-active-directory,asp.net-core-webapi,openid,azure-ad-b2c,msal,Azure Active Directory,Asp.net Core Webapi,Openid,Azure Ad B2c,Msal,我已经为WebApp和Api设置了身份验证/授权,并且工作正常。问题是当我必须引入额外的Api时,这些Api将从WebAPP调用 限制是您不能在一次调用中请求作用域混合Web API的令牌。这是服务(AAD)的限制,而不是图书馆的限制。 您必须为https://{tenant}.onmicrosoft.com/api1/read请求一个令牌 然后您可以为https://{tenant}.onmicrosoft.com/api2/read静默地获取令牌,因为这是两个不同的API。 我从和中学到了更
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
services.AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options)).AddCookie();
AddAzureAdB2C是来自的自定义扩展方法
以下是api客户端配置
services.AddHttpClient<IApiClient1, ApiClient1>()
.AddHttpMessageHandler<API1AccessTokenHandler>();
services.AddHttpClient<IApiClient2, ApiClient2>()
.AddHttpMessageHandler<API2AccessTokenHandler>();
是的,我们可以为同一个api设置多个作用域,而不是来自不同api的多个作用域 在中,我们检索具有指定作用域的令牌
// Retrieve the token with the specified scopes
var scope = new string[] { api1_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;
您可以获得具有不同api作用域的accessToken
// Retrieve the token with the specified scopes
var scope = new string[] { api2_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;
是的,我们可以为同一个api设置多个作用域,而不是来自不同api的多个作用域 在中,我们检索具有指定作用域的令牌
// Retrieve the token with the specified scopes
var scope = new string[] { api1_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;
您可以获得具有不同api作用域的accessToken
// Retrieve the token with the specified scopes
var scope = new string[] { api2_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;
试试这里的样品。您可以使用两个不同的api作为作用域。我已经尝试过这样做,它会引发错误,您可以为同一个api使用多个作用域,而不是来自不同api的多个作用域。你能提供一个例子或文章,使用不同领域的多个作用域吗apis@Mady你找到解决办法了吗?我们有相同的问题,但在ASP.NETTry中有此示例。您可以使用两个不同的api作为作用域。我已经尝试过这样做,它会引发错误,您可以为同一个api使用多个作用域,而不是来自不同api的多个作用域。你能提供一个例子或文章,使用不同领域的多个作用域吗apis@Mady你找到解决办法了吗?我们有同样的问题,但在ASP.NET中,我已经在做了,请看API1AccessTokenHandler和API2AccessTokenHandler。我已经在做了,请看API1AccessTokenHandler和API2AccessTokenHandler。
cca.AcquireTokenByAuthorizationCode(AzureAdB2COptions.ApiScopes.Split(' '), code)
.WithExtraScopeToConsent(additionalScopeForAPi2)
.ExecuteAsync();
// Retrieve the token with the specified scopes
var scope = new string[] { api1_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;
// Retrieve the token with the specified scopes
var scope = new string[] { api2_scope };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault()).ExecuteAsync();
var accessToken=result.AccessToken;