我成功设置了多租户应用程序。 目前,我能够对用户进行身份验证并使用令牌来访问其他资源。 (Microsoft Graph& Microsoft AD Graph)
现在我想让B2B工作。 当前流程: - 用户登录 - AuthorizationCodeReceived获取获取令牌(通过$ commonAuthority端点) - 在请求广告图表的令牌时,我使用的是$ tenantAuthority
当$ tenantAuthority与创建帐户的租户权限相同时,这非常有效。
但是,如果我使用其他用户(来自其他租户,信任实际租户)并使用$ tenantAuthority = trusted authority登录,那么我总是出现以下错误: 刷新令牌失败: AADSTS65001:用户或管理员未同意使用ID为
的应用程序如果我将$ tenantAuthority更改为创建用户的'source'租户权限,一切正常。
非常感谢任何帮助。
更新:代码示例
App有两个租户(tenantA和tenantB),我将使用tenantB中的用户和tenantA给予该用户信任。
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的应用程序。
答案 0 :(得分:0)
我没有看到你提到这一点:在B2B合作中,你首先要邀请其他租户的用户。步骤是这样的:
这在我的案例中完美无缺。
关于我发现的一些问题:
在活动目录资源末尾追踪“/” - 尝试删除它,因为这可能会导致问题。您将找到一些代码来获取身份验证标头:
string aadTenant = WebServiceClientConfiguration.Settings.ActiveDirectoryTenant;
string clientAppId = WebServiceClientConfiguration.Settings.ClientAppId;
string clientKey = WebServiceClientConfiguration.Settings.ClientKey;
string aadResource = WebServiceClientConfiguration.Settings.ActiveDirectoryResource;
AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant);
ClientCredential clientCredential = new ClientCredential(clientAppId, clientKey);
UserPasswordCredential upc = new UserPasswordCredential(WebServiceClientConfiguration.Settings.UserName, WebServiceClientConfiguration.Settings.Password);
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(aadResource, clientAppId, upc);
return authenticationResult.CreateAuthorizationHeader();
默认情况下,Azure AD中配置的应用程序无法使用OAuth2隐式授权。您需要明确选择加入 - 可在此处找到更多详细信息:Azure AD OAuth2 implicit grant
答案 1 :(得分:0)
在检索令牌时更改catch方法可以解决问题:
if(e.ErrorCode == "failed_to_acquire_token_silently")
{
HttpContext.Current.Response.Redirect(authenticationContext.GetAuthorizationRequestUrlAsync(resourceId, _configuration.ClientId, new Uri(currentUrl),
new UserIdentifier(currentUserId, UserIdentifierType.UniqueId), string.Empty);
}