Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Enable the possibility to specify TenantId #170

Closed
brekkjen opened this issue Oct 18, 2021 · 5 comments · Fixed by #169 or #178
Closed

[Feature] Enable the possibility to specify TenantId #170

brekkjen opened this issue Oct 18, 2021 · 5 comments · Fixed by #169 or #178

Comments

@brekkjen
Copy link
Contributor

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
Copy link
Member

shweaver-MSFT commented Oct 25, 2021

#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
Copy link
Contributor Author

brekkjen commented Dec 3, 2021

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
Copy link
Member

shweaver-MSFT commented Dec 3, 2021

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
Copy link
Contributor Author

brekkjen commented Dec 4, 2021

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

@shweaver-MSFT
Copy link
Member

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

@ghost ghost locked as resolved and limited conversation to collaborators Jan 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants