我已成功设置多租户应用程序。现在,我能够对用户进行身份验证并使用令牌来访问其他资源。(Microsoft 图形和 Microsoft AD 图形)
现在我想让 B2B 工作。当前流程: - 用户登录 - AuthorizationCodeReceived 获取获取令牌(通过 $commonAuthority 端点) - 在为广告图请求令牌时,我使用 $tenantAuthority
当 $tenantAuthority 与创建帐户的租户权限相同时,这非常有效。
但是,如果我使用另一个用户(来自另一个租户,信任实际租户)登录并使用 $tenantAuthority = 受信任的权限,那么我总是出现以下错误:刷新令牌失败:AADSTS65001:用户或管理员未同意使用带有 ID 的应用程序
如果我将 $tenantAuthority 更改为创建用户的“源”租户权限,一切正常。
任何帮助将不胜感激。
更新:代码示例
应用程序有两个租户(租户 A 和租户 B),我将使用来自租户 B 的用户,租户 A 信任该用户。
AuthorizationCodeReceived = async context =>
{
TenantContext.TenantId = "someguid";
var tenantId =
TenantContext.TenantId;
// get token cache via func, because the userid is only known at runtime
var getTokenCache = container.Resolve<Func<string, TokenCache>>();
var userId = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.ObjectIdentifier).Value;
var tokenCache = getTokenCache(userId);
var authenticationContext = new AuthenticationContext($"{configuration.Authority}",
tokenCache);
await authenticationContext.AcquireTokenByAuthorizationCodeAsync(
context.Code,
new Uri(context.Request.Uri.GetLeftPart(UriPartial.Authority)),
new ClientCredential(configuration.ClientId, configuration.ClientSecret),
configuration.GraphResourceId);
}
此代码完美运行。使用来自两个租户的用户登录效果很好。
但是当我需要 Graph Service Client 或 ActiveDirectoryClient 时,我需要获取访问令牌才能为某个租户处理 api。我像这样检索访问令牌:
public IGraphServiceClient CreateGraphServiceClient()
{
var client = new GraphServiceClient(
new DelegateAuthenticationProvider(
async requestMessage =>
{
Logger.Debug("Retrieving authentication token to use in Microsoft Graph.");
string token;
var currentUserHomeTenantId = TenantContext.TenantId;
var currentUserObjectId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.ObjectIdentifier).Value;
var authenticationContext =
new AuthenticationContext($"{_configuration.TenantAuthorityPrefix}{currentUserHomeTenantId}",
_tokenCacheFactoryMethod(currentUserObjectId));
var clientCredential = new ClientCredential(_configuration.ClientId, _configuration.ClientSecret);
try
{
token = await GetTokenSilently(authenticationContext, _configuration.GraphResourceId, currentUserObjectId);
}
catch (AdalSilentTokenAcquisitionException e)
{
Logger.Error("Failed to retrieve authentication token silently, trying to refresh the token.", e);
var result = await authenticationContext.AcquireTokenAsync(_configuration.GraphResourceId, clientCredential);
token = result.AccessToken;
}
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AuthenticationHeaderKeys.Bearer, token);
}));
return client;
}
public IActiveDirectoryClient CreateAdClient()
{
var currentUserHomeTenantId = TenantContext.TenantId;
var currentUserObjectId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.ObjectIdentifier).Value;
var graphServiceUrl = $"{_configuration.AdGraphResourceId}/{currentUserHomeTenantId}";
var tokenCache = _tokenCacheFactoryMethod(currentUserObjectId);
var client = new ActiveDirectoryClient(new Uri(graphServiceUrl),
() => GetTokenSilently(
new AuthenticationContext(
$"{_configuration.TenantAuthorityPrefix}{ClaimsPrincipal.Current.FindFirst(ClaimTypes.TenantId).Value}", tokenCache
),
_configuration.AdGraphResourceId, currentUserObjectId
));
return client;
}
当我使用两个客户端 SDK 之一发出请求时,出现以下错误:刷新令牌失败:AADSTS65001:用户或管理员未同意使用带有 ID 的应用程序。