Skip to content

Instantly share code, notes, and snippets.

@luismanez
Created May 3, 2021 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luismanez/e17d7a415baf2def2833c2fe8bc4b80e to your computer and use it in GitHub Desktop.
Save luismanez/e17d7a415baf2def2833c2fe8bc4b80e to your computer and use it in GitHub Desktop.
PnPCore sample using multiple Auth providers
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PnP.Core.Auth;
using PnP.Core.Auth.Services.Builder.Configuration;
using PnP.Core.Services;
using PnP.Core.Services.Builder.Configuration;
namespace pnp_core_lab
{
class Program
{
public static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
})
.ConfigureServices((hostingContext, services) =>
{
services.AddPnPCore(options =>
{
options.PnPContext.GraphFirst = true;
options.HttpRequests.UserAgent = "ISV|Contoso|ProductX";
options.HttpRequests.Timeout = 100;
options.HttpRequests.MicrosoftGraph.MaxRetries = 2;
options.HttpRequests.SharePointRest.MaxRetries = 3;
options.HttpRequests.SharePointRest.UseIncrementalDelay = true;
options.Sites.Add("DemoPnP", new PnPCoreSiteOptions
{
SiteUrl = "https://tenant.sharepoint.com/sites/team1"
});
options.Sites.Add("BatTeam", new PnPCoreSiteOptions
{
SiteUrl = "https://tenant.sharepoint.com/sites/batman"
});
});
services.AddPnPCoreAuthentication(options =>
{
options.Credentials.Configurations.Add("interactive",
new PnPCoreAuthenticationCredentialConfigurationOptions
{
ClientId = "{clientId}",
TenantId = "{tenantId}",
Interactive = new PnPCoreAuthenticationInteractiveOptions
{
RedirectUri = new Uri("http://localhost")
}
});
options.Credentials.Configurations.Add("devicecode",
new PnPCoreAuthenticationCredentialConfigurationOptions
{
ClientId = "{clientId}",
TenantId = "{tenantId}",
DeviceCode = new PnPCoreAuthenticationDeviceCodeOptions
{
RedirectUri = new Uri("http://localhost")
}
});
options.Credentials.DefaultConfiguration = "devicecode";
// Map the site defined in AddPnPCore with the Authentication Provider configured in this action
options.Sites.Add("DemoPnP",
new PnPCoreAuthenticationSiteOptions
{
AuthenticationProviderName = "interactive"
});
options.Sites.Add("BatTeam",
new PnPCoreAuthenticationSiteOptions
{
AuthenticationProviderName = "devicecode"
});
});
})
.UseConsoleLifetime()
.Build();
await host.StartAsync();
using (var scope = host.Services.CreateScope())
{
var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();
using (var context = await pnpContextFactory.CreateAsync("DemoPnP"))
{
var web = await context.Web.GetAsync();
Console.WriteLine($"Title: {web.Title}");
}
using (var context = await pnpContextFactory.CreateAsync("BatTeam",
(authProvider) => {
((DeviceCodeAuthenticationProvider)authProvider)
.DeviceCodeVerification = DeviceCodeVerificationCallback;
}))
{
var web = await context.Web.GetAsync();
Console.WriteLine($"Title: {web.Title}");
}
}
host.Dispose();
}
private static void DeviceCodeVerificationCallback(DeviceCodeNotification notification)
{
// Write the Device Code in the Output window
Console.WriteLine($"{notification.Message}");
Console.WriteLine($"{notification.UserCode}");
// Start the browser to input the Device Code
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo
{
FileName = notification.VerificationUrl.ToString(),
UseShellExecute = true
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment