Exemple #1
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task TestUsernamePasswordConstructorNoDI_NullUsername()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            var provider = new UsernamePasswordAuthenticationProvider(
                AuthGlobals.DefaultClientId,
                AuthGlobals.OrganizationsTenantId,
                null,
                "FakePassword".ToSecureString());
        }
Exemple #2
0
        public async Task TestUsernamePasswordGetAccessTokenAsyncFullNullScopes()
        {
            var provider = new UsernamePasswordAuthenticationProvider(
                AuthGlobals.DefaultClientId,
                AuthGlobals.OrganizationsTenantId,
                "FakeUsername",
                "FakePassword".ToSecureString());

            await provider.GetAccessTokenAsync(TestGlobals.GraphResource, null);
        }
Exemple #3
0
        public async Task TestUsernamePasswordAuthenticateRequestAsyncNoHttpRequest()
        {
            var provider = new UsernamePasswordAuthenticationProvider(
                AuthGlobals.DefaultClientId,
                AuthGlobals.OrganizationsTenantId,
                "FakeUsername",
                "FakePassword".ToSecureString());

            await provider.AuthenticateRequestAsync(TestGlobals.GraphResource, null);
        }
Exemple #4
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task TestUsernamePasswordConstructorNoDI_NullClientId_NullTenantId()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            var provider = new UsernamePasswordAuthenticationProvider(
                null,
                null,
                "FakeUsername",
                "FakePassword".ToSecureString());

            Assert.IsNotNull(provider);
            Assert.IsNotNull(provider.ClientId);
            Assert.IsNotNull(provider.TenantId);
            Assert.IsNotNull(provider.Username);
            Assert.IsNotNull(provider.Password);
        }
Exemple #5
0
        public async Task TestUsernamePasswordGetAccessTokenAsyncCorrect()
        {
            if (TestCommon.RunningInGitHubWorkflow())
            {
                Assert.Inconclusive("Skipping live test because we're running inside a GitHub action");
            }

            var configuration = TestCommon.GetConfigurationSettings();
            var username      = configuration.GetValue <string>($"{TestGlobals.CredentialsConfigurationBasePath}:{usernamePasswordConfigurationPath}:UsernamePassword:Username");
            var password      = configuration.GetValue <string>($"{TestGlobals.CredentialsConfigurationBasePath}:{usernamePasswordConfigurationPath}:UsernamePassword:Password");

            var provider = new UsernamePasswordAuthenticationProvider(
                AuthGlobals.DefaultClientId,
                AuthGlobals.OrganizationsTenantId,
                username,
                password.ToSecureString());

            var accessToken = await provider.GetAccessTokenAsync(TestGlobals.GraphResource);

            Assert.IsNotNull(accessToken);
            Assert.IsTrue(accessToken.Length > 0);
        }
Exemple #6
0
        public async Task TestUsernamePasswordAuthenticateRequestAsyncCorrect()
        {
            if (TestCommon.RunningInGitHubWorkflow())
            {
                Assert.Inconclusive("Skipping live test because we're running inside a GitHub action");
            }

            var configuration = TestCommon.GetConfigurationSettings();
            var username      = configuration.GetValue <string>($"{TestGlobals.CredentialsConfigurationBasePath}:{usernamePasswordConfigurationPath}:UsernamePassword:Username");
            var password      = configuration.GetValue <string>($"{TestGlobals.CredentialsConfigurationBasePath}:{usernamePasswordConfigurationPath}:UsernamePassword:Password");

            var provider = new UsernamePasswordAuthenticationProvider(
                AuthGlobals.DefaultClientId,
                AuthGlobals.OrganizationsTenantId,
                username,
                password.ToSecureString());

            var request = new HttpRequestMessage(HttpMethod.Get, TestGlobals.GraphMeRequest);
            await provider.AuthenticateRequestAsync(TestGlobals.GraphResource, request);

            Assert.IsNotNull(request.Headers.Authorization);
            Assert.AreEqual(request.Headers.Authorization.Scheme.ToLower(), "bearer");
        }
Exemple #7
0
        public static async Task Main(string[] args)
        {
            var host = Host.CreateDefaultBuilder()

                       // Ensure you do consent to the PnP App when using another tenant (update below url to match your aad domain):
                       // https://login.microsoftonline.com/a830edad9050849523e17050400.onmicrosoft.com/adminconsent?client_id=31359c7f-bd7e-475c-86db-fdb8c937548e&state=12345&redirect_uri=https://www.pnp.com
                       //.UseEnvironment("officedevpnp")
                       .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConsole();
            })
                       .ConfigureServices((hostingContext, services) =>
            {
                var customSettings = new CustomSettings();
                hostingContext.Configuration.Bind("CustomSettings", customSettings);

                //Create an instance of the Authentication Provider that uses Credential Manager
                var authenticationProvider = new UsernamePasswordAuthenticationProvider(
                    customSettings.ClientId,
                    customSettings.TenantId,
                    customSettings.UserPrincipalName,
                    StringToSecureString(customSettings.Password));

                services.AddPnPCore(options =>
                {
                    options.DefaultAuthenticationProvider = authenticationProvider;

                    options.Sites.Add("DemoSite",
                                      new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions
                    {
                        SiteUrl = customSettings.DemoSiteUrl,
                        AuthenticationProvider = authenticationProvider
                    });
                });
            })
                       // Let the builder know we're running in a console
                       .UseConsoleLifetime()
                       // Add services to the container
                       .Build();

            await host.StartAsync();

            using (var scope = host.Services.CreateScope())
            {
                var pnpContextFactory = scope.ServiceProvider.GetRequiredService <IPnPContextFactory>();

                #region Interactive GET's
                using (var context = await pnpContextFactory.CreateAsync("DemoSite"))
                {
                    // ================================================================
                    // Getting data - everything is async!
                    // Same programming model, regardless of wether under the covers Microsoft Graph is used or SharePoint REST

                    // Interactive GET samples

                    // Retrieving web with lists and masterpageurl loaded ==> SharePoint REST query
                    var web = await context.Web.GetAsync(p => p.Title, p => p.Lists, p => p.MasterUrl);

                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("===Web (REST)===");
                    Console.WriteLine($"Title: {web.Title}");
                    Console.WriteLine($"# Lists: {web.Lists.Length}");
                    Console.ResetColor();
                }
                #endregion
            }

            host.Dispose();
        }