CommunityToolkit / Graph-Controls

Set of Helpers and Controls for Windows development using the Microsoft Graph.
https://docs.microsoft.com/en-us/windows/communitytoolkit/graph/overview
Other
155 stars 39 forks source link

[Feature] Enable the possibility to specify TenantId #170

Closed brekkjen closed 2 years ago

brekkjen commented 2 years ago

Describe the problem this feature would solve

I cant find a way to specify TenantId when creating the GlobalProvider

Describe the solution

Please make it possible to specify TenantId

Describe alternatives you've considered

Additional context & Screenshots

shweaver-MSFT commented 2 years ago

169 adds the ability to specify the TenantId in the MsalProvider constructor:

/// <summary>
/// Initializes a new instance of the <see cref="MsalProvider"/> class with default configuration values.
/// </summary>
/// <param name="clientId">Registered client id in Azure Acitve Directory.</param>
/// <param name="redirectUri">RedirectUri for auth response.</param>
/// <param name="scopes">List of Scopes to initially request.</param>
/// <param name="autoSignIn">Determines whether the provider attempts to silently log in upon creation.</param>
/// <param name="listWindowsWorkAndSchoolAccounts">Determines if organizational accounts should be enabled/disabled.</param>
/// <param name="tenantId">Registered tenant id in Azure Active Directory.</param>
public MsalProvider(string clientId, string[] scopes = null, string redirectUri = null, bool autoSignIn = true, bool listWindowsWorkAndSchoolAccounts = true, string tenantId = null) { ... }

WindowsProvider has yet to be evaluated/updated.

brekkjen commented 2 years ago

Just tested this and I'm getting an error if providing TenantId because it will cause both TenantId and WithAuthority to be specified and those two are mutually exlusive. I solved it like this:

protected IPublicClientApplication CreatePublicClientApplication(string clientId, string tenantId, string redirectUri, bool listWindowsWorkAndSchoolAccounts)
        {
            var authority = listWindowsWorkAndSchoolAccounts ? AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount : AadAuthorityAudience.PersonalMicrosoftAccount;

            var clientBuilder = PublicClientApplicationBuilder.Create(clientId)
                //.WithAuthority(AzureCloudInstance.AzurePublic, authority)
                .WithClientName(ProviderManager.ClientName)
                .WithClientVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString());

            if (tenantId != null)
            {
                clientBuilder = clientBuilder.WithTenantId(tenantId);
            }
            else
            {
                clientBuilder = clientBuilder.WithAuthority(AzureCloudInstance.AzurePublic, authority);
            }

#if WINDOWS_UWP || NET5_0_WINDOWS10_0_17763_0
            clientBuilder = clientBuilder.WithBroker();
#elif NETCOREAPP3_1
            clientBuilder = clientBuilder.WithWindowsBroker();
#endif

            clientBuilder = (redirectUri != null)
                ? clientBuilder.WithRedirectUri(redirectUri)
                : clientBuilder.WithDefaultRedirectUri();

            return clientBuilder.Build();
        }
shweaver-MSFT commented 2 years ago

Hey, great catch @brekkjen. Any interest in submitting a PR? If not I can do it this afternoon. But since you have the solution already I figured I'd check with you first :)

brekkjen commented 2 years ago

Done, hope I'm doing this right... My first PR :)

shweaver-MSFT commented 2 years ago

Great work Leif, and thanks for getting involved with the project! ⭐🚀