Exemplo n.º 1
0
    private static PrincipalContext GetPrincipalContext(string domainName, ActiveDirectoryConfigurationEmbedded config)
    {
        if (config.DirectoryRegistry_Username.HasText())
        {
            return(new PrincipalContext(ContextType.Domain, domainName, config.DirectoryRegistry_Username + "@" + config.DomainServer, config.DirectoryRegistry_Password));
        }

        return(new PrincipalContext(ContextType.Domain, domainName)); //Uses current user
    }
    public static ClientCredentialProvider GetAuthProvider(this ActiveDirectoryConfigurationEmbedded activeDirectoryConfig, string[]?scopes = null)
    {
        IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                                                                       .Create(activeDirectoryConfig.Azure_ApplicationID)
                                                                       .WithTenantId(activeDirectoryConfig.Azure_DirectoryID)
                                                                       .WithClientSecret(activeDirectoryConfig.Azure_ClientSecret)
                                                                       .Build();

        var authResultDirect = confidentialClientApplication.AcquireTokenForClient(scopes ?? new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync().Result;

        //Microsoft.Graph.Auth is required for the following to work
        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

        return(authProvider);
    }