Azure ad b2c AcquireTokenSilent用于使用Azure B2C和MSAL.NET的多个WebAPI访问令牌
我们有一个web应用程序,它需要对多个web API进行身份验证访问。我们正在使用Azure AD B2C进行身份验证 我理解您不能在一个调用中同时包含两个资源的作用域 我理解MSAL是要使用从第一个资源令牌缓存的刷新令牌来请求第二个资源的访问令牌Azure ad b2c AcquireTokenSilent用于使用Azure B2C和MSAL.NET的多个WebAPI访问令牌,azure-ad-b2c,msal,Azure Ad B2c,Msal,我们有一个web应用程序,它需要对多个web API进行身份验证访问。我们正在使用Azure AD B2C进行身份验证 我理解您不能在一个调用中同时包含两个资源的作用域 我理解MSAL是要使用从第一个资源令牌缓存的刷新令牌来请求第二个资源的访问令牌 IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication(); var accounts = await cca.GetAccount
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scopeForApi2, accounts.FirstOrDefault())
进行此调用时,调用不会失败,但访问令牌为空
在Azure B2C中,web应用程序和WebAPI已注册,并且web应用程序已被授予对WebAPI中所有作用域的权限和管理员同意
Azure AD B2C和/或MSAL.NET是否支持此功能
我们成功获取了第一个资源api的第一个访问令牌:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// Generate the metadata address using the tenant and policy information
MetadataAddress = String.Format(Globals.WellKnownMetadata, Globals.Tenant, Globals.DefaultPolicy),
// These are standard OpenID Connect parameters, with values pulled from web.config
ClientId = Globals.ClientId,
RedirectUri = Globals.RedirectUri,
PostLogoutRedirectUri = Globals.RedirectUri,
// Specify the callbacks for each type of notifications
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = OnRedirectToIdentityProvider,
AuthorizationCodeReceived = OnAuthorizationCodeReceived,
AuthenticationFailed = OnAuthenticationFailed,
},
// Specify the claim type that specifies the Name property.
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
ValidateIssuer = false
},
// Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
Scope = $"openid profile offline_access {Globals.ReadTasksScopeApi1} {Globals.WriteTasksScopeApi1}"
}
);
我们使用authorizationcode获得第一个访问令牌-我们接收一个访问令牌和刷新令牌
IConfidentialClientApplication confidentialClient
= MsalAppBuilder.BuildConfidentialClientApplication(
new ClaimsPrincipal(notification.AuthenticationTicket.Identity));
// Upon successful sign in, get & cache a token using MSAL
AuthenticationResult result = await confidentialClient
.AcquireTokenByAuthorizationCode(Globals.Scopes_Api1, notification.Code)
.ExecuteAsync();
我还尝试添加“resource”查询参数,但似乎没有任何区别:
var scope = new string[] { Globals.ReadTasksScopeApi2 };
IConfidentialClientApplication cca = MsalAppBuilder.BuildConfidentialClientApplication();
var accounts = await cca.GetAccountsAsync();
AuthenticationResult result = await cca.AcquireTokenSilent(scope, accounts.FirstOrDefault())
.WithExtraQueryParameters(new Dictionary<string, string>
{
// { "resource", "https://myb2c.onmicrosoft.com/api2" }, // tried appid and guid
{ "resource", "0a6ab6b5-1b88-49fe-a6cf-19f1878d3508" }
})
.ExecuteAsync();
var scope=新字符串[]{Globals.readtaskscopeapi2};
IConfidentialClientApplication cca=MsalAppBuilder.buildSecretentialClientApplication();
var accounts=wait cca.GetAccountsAsync();
AuthenticationResult=await cca.AcquireTokenSilent(作用域、accounts.FirstOrDefault())
.使用ExtraQueryParameters(新字典
{
//{“资源”https://myb2c.onmicrosoft.com/api2},//尝试了appid和guid
{“资源”,“0a6ab6b5-1b88-49fe-a6cf-19f1878d3508”}
})
.ExecuteAsync();
为什么在使用Azure AD B2C的第二个资源上第二次调用AcquireTokenSilent时访问令牌为空?2/19/2020
我已向Azure支持工程师确认Azure AD B2C不支持此类场景。您传入的范围名称是什么
scopeForApi2
我们有两个api,它们的应用程序ID uri如下:和。第一个api的作用域如下:。第二个api已经实现了。在scopeForApi2变量中,我们要求它与标准AzureAD目录一起工作,但不适用于Azure AD B2C。我可以重现此问题,将进行进一步研究。是否使用WithB2CAuthority()构建机密客户端应用程序
?