1

マルチテナント アプリケーションのセットアップに成功しました。今のところ、ユーザーを認証し、トークンを使用して他のリソースにアクセスできます。(マイクロソフト グラフ & マイクロソフト AD グラフ)

今、私はB2Bを機能させたいと思っています。現在の流れ: - ユーザーがサインインする - AuthorizationCodeReceived がトークンを取得する ($commonAuthority エンドポイント経由) - Ad Graph のトークンをリクエストするときは、$tenantAuthority を使用しています

$tenantAuthority が、アカウントが作成されたものと同じテナント機関である場合、これは完全に機能します。

ただし、別のユーザー (別のテナントから、実際のテナントへの信頼が与えられている) でログインし、$tenantAuthority = 信頼された機関を使用すると、常に次のエラーが発生します: 更新トークンに失敗しました: AADSTS65001: ユーザーまたは管理者が同意していませんIDでアプリを利用する

$tenantAuthority をユーザーが作成された「ソース」テナント機関に変更すると、すべて正常に機能します。

どんな助けでも大歓迎です。

更新: コード サンプル

アプリには 2 つのテナント (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;
    }

2 つのクライアント SDK のいずれかで要求を行うと、次のエラーが発生しました: 更新トークンに失敗しました: AADSTS65001: ユーザーまたは管理者は、ID でアプリケーションを使用することに同意しませんでした。

4

2 に答える 2

1

トークンを取得するときに 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);
}
于 2016-11-21T08:58:19.060 に答える