public static ClientContext GetTenantContext() { var authManager = new OfficeDevPnP.Core.AuthenticationManager(); var context = authManager.GetWebLoginClientContext(adminSiteUrl); //ClientContext context = new ClientContext(adminSiteUrl) //{ // Credentials = new SharePointOnlineCredentials(userName, GetSecurePassword()), //}; return(context); }
//gavdcodeend 02 //gavdcodebegin 06 static void LoginPnPCoreMFA() { string siteUrl = ConfigurationManager.AppSettings["spUrl"]; var authManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext mfaContext = authManager.GetWebLoginClientContext(siteUrl); Web myWeb = mfaContext.Web; mfaContext.Load(myWeb, wb => wb.Title); mfaContext.ExecuteQuery(); Console.WriteLine("Connected to the site " + myWeb.Title + " using MFA"); }
static void Main(string[] args) { string siteUrl = GetSite(); // Console.WriteLine("Enter credentials for {0}", siteUrl); string userName = GetUserName(); SecureString pwd = GetPassword(); if (string.IsNullOrEmpty(userName) || (pwd == null)) { return; } try { Console.WriteLine("Checking Default Authentication"); ClientContext ctx = new ClientContext(siteUrl); ctx.AuthenticationMode = ClientAuthenticationMode.Default; ctx.Credentials = new SharePointOnlineCredentials(userName, pwd); Web web = ctx.Web; ctx.Load(web); ctx.ExecuteQuery(); Console.WriteLine("the current web : " + web.Title); Console.WriteLine("Default Authentication successed"); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { Console.WriteLine("Checking Multi Authentication"); var authenticationManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext context = authenticationManager.GetWebLoginClientContext(siteUrl, null); Web web = context.Web; context.Load(web); context.ExecuteQuery(); Console.WriteLine("the current web : " + web.Title); Console.WriteLine("Multi Athentication Authentication successed"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }
public static ClientContext GetClientContext() { var authManager = new OfficeDevPnP.Core.AuthenticationManager(); var context = authManager.GetWebLoginClientContext(siteUrl); //Console.WriteLine("Enter UserName"); //string userName = Console.ReadLine(); // //For On-Premise //CredentialCache cc = new CredentialCache(); //cc.Add(new Uri("SITEURL"), "NTLM", CredentialCache.DefaultNetworkCredentials); //ClientContext context = new ClientContext(SiteUrl) //{ // Credentials = cc, // AuthenticationMode = ClientAuthenticationMode.Default // // with Windows Authentication // Credentials = CredentialCache.DefaultNetworkCredentials, // // With custom Credential with Domain // Credentials = new NetworkCredential("USERNAME", "PASSWORD", "DOMAINNAME") // // With custom Credential without Domain // Credentials = new NetworkCredential("USERNAMEt", "PASSWORD") // // Form based Authentication // AuthenticationMode = ClientAuthenticationMode.FormsAuthentication, // FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("USERNAME", "PASSWORD") //}; // //For Office 365 - SharePoint Online //ClientContext context = new ClientContext(siteUrl) //{ // Credentials = new SharePointOnlineCredentials(userName, GetPassword()), //}; return(context); }
static void Main(string[] args) { Console.WriteLine("Enter your SharePoint Online Admin Center URL"); string adminCenterUrl = Console.ReadLine(); var authManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext ctx = authManager.GetWebLoginClientContext(adminCenterUrl); Console.WriteLine("\nYou are connected to " + adminCenterUrl + "\n"); Console.WriteLine("\nEnter your SharePoint App Catalog URL"); string appCatalogUrl = Console.ReadLine(); var settings = TenantSettings.GetCurrent(ctx); settings.SetCorporateCatalog(appCatalogUrl); ctx.ExecuteQuery(); Console.WriteLine("\nYou changed your App Catalog to " + appCatalogUrl); }
private static void LoginUsingM365() { string siteURL; bool isValidTenantURL; //Retrieve Admin Site URL from User via Console Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Please enter the M365 Admin Site URL."); Console.ResetColor(); //Verify it's a valid Tenant Site URL do { siteURL = Console.ReadLine(); isValidTenantURL = ValidateTenantURL(siteURL); } while (!isValidTenantURL); //Authenticate using M365 Pop Up Auth Window ( to handle MFA too ) var authenticationManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext context = authenticationManager.GetWebLoginClientContext(siteURL, null); GetAllTenantSites(context); }
static void Main(string[] args) { Console.WriteLine("Enter your SharePoint Online Site URL"); string siteURL = Console.ReadLine(); var authManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext ctx = authManager.GetWebLoginClientContext(siteURL); Console.WriteLine("\nYou are connected to " + siteURL + "\n"); Console.WriteLine("\nEnter your List Name"); string listName = Console.ReadLine(); Console.WriteLine("\nEnter your Field Internal Name"); string fieldInternalName = Console.ReadLine(); Console.WriteLine("\nEnter your Field Display Name"); string fieldDisplayName = Console.ReadLine(); Web web = ctx.Web; ctx.Load(web); List list = web.Lists.GetByTitle(listName); ctx.Load(list); ctx.ExecuteQuery(); string schemaTextField = string.Format("<Field Type='Text' Name='{0}' StaticName='{0}' DisplayName='{1}' />", fieldInternalName, fieldDisplayName); Field simpleTextField = list.Fields.AddFieldAsXml(schemaTextField, true, AddFieldOptions.AddFieldInternalNameHint); ctx.ExecuteQuery(); Console.WriteLine("\nField '" + fieldDisplayName + "' was added successfully"); Console.ReadKey(); }
static void Main(string[] args) { try { ConsoleColor defaultForeground = Console.ForegroundColor; // Collect information // string templateWebUrl = GetInput("Enter the URL of the template site: ", false, defaultForeground); string targetWebUrl = GetInput("Enter the URL of the target site: ", false, defaultForeground); string userName = GetInput("Enter your user name:", false, defaultForeground); string pwdS = GetInput("Enter your password:"******"Get XMl Path:", false, defaultForeground); string filename = GetInput("Get XMl filename:", false, defaultForeground); Console.Write("\nPress 1 for Default authentication . Press 2 for Multi-Factor Authentication "); string auth = Console.ReadLine(); SecureString pwd = new SecureString(); foreach (char c in pwdS.ToCharArray()) { pwd.AppendChar(c); } if (auth.Equals("2")) { var authenticationManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext pnpcontext = authenticationManager.GetWebLoginClientContext(targetWebUrl, null); Web pnpweb = pnpcontext.Web; pnpcontext.Load(pnpweb); pnpcontext.ExecuteQuery(); XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(filepath, ""); ProvisioningTemplate template = provider.GetTemplate(filename); ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation(); ptai.ProgressDelegate = delegate(String message, Int32 progress, Int32 total) { Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message); }; template.Connector = provider.Connector; pnpweb.ApplyProvisioningTemplate(template, ptai); } else { using (var context = new ClientContext(targetWebUrl)) { context.Credentials = new SharePointOnlineCredentials(userName, pwd); Web web = context.Web; context.Load(web, w => w.Title); context.ExecuteQueryRetry(); XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(filepath, ""); ProvisioningTemplate template = provider.GetTemplate(filename); ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation(); ptai.ProgressDelegate = delegate(String message, Int32 progress, Int32 total) { Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message); }; template.Connector = provider.Connector; web.ApplyProvisioningTemplate(template, ptai); } // Get the template from existing site and serialize that (not really needed) // Just to pause and indicate that it's all done } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("We are all done. Press enter to continue."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } }
/// <summary> /// Ask the User for his Credentials.... Not the best idea in automated Scripts /// </summary> public static ClientContext authenticateByAskingUser() { OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager(); return(authMgr.GetWebLoginClientContext(siteURL)); }
private void cmdLogin_Click(object sender, EventArgs e) { try { EnableUI(false); if (!scSites.Contains(cboSites.Text)) { scSites.Add(cboSites.Text); } LoginContext = new Microsoft.SharePoint.Client.ClientContext(cboSites.Text); SecureString password = GetSecureString(txtPassword.Text); switch (GetAuthMode()) { case "Basic": if (!string.IsNullOrEmpty(txtDomain.Text)) { LoginContext.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text, txtDomain.Text); } else { LoginContext.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text); } break; case "SPO": LoginContext.Credentials = new spc.SharePointOnlineCredentials(txtUserName.Text, password); break; case "PNP": OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager(); ClientContext ctx = authManager.GetWebLoginClientContext(cboSites.Text); //ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(cboSites.Text, txtUserName.Text,password); break; default: break; } LoginContext.ExecutingWebRequest += delegate(object sender2, WebRequestEventArgs e2) { e2.WebRequestExecutor.WebRequest.UserAgent = "NONISV|RammWare|spToolbelt2019/1.0"; }; spc.Web oRootWeb = LoginContext.Site.RootWeb; LoginContext.Load(oRootWeb); LoginContext.ExecuteQuery(); System.Diagnostics.Trace.WriteLine(oRootWeb.Title); UserLoggedIn = true; SaveSettings(); } catch (Exception ex) { ErrorHandling.LogError(ex, "frmLogin.cmdLogin_Click", ""); } }