public SPClientContext(Uri webFullUrl, AuthType authType = AuthType.Default, string userName = null, string password = null) : this(webFullUrl) { Authentication = authType; UserName = userName; switch (Authentication) { case AuthType.Default: AuthenticationMode = ClientAuthenticationMode.Default; Credentials = string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(password) ? CredentialCache.DefaultNetworkCredentials : new NetworkCredential(UserName, password); break; case AuthType.SharePointOnline: AuthenticationMode = ClientAuthenticationMode.Default; Credentials = new SharePointOnlineCredentials(UserName, Utility.GetSecureString(password)); break; case AuthType.Anonymous: AuthenticationMode = ClientAuthenticationMode.Anonymous; break; case AuthType.Forms: AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(UserName, password); break; } }
static void Main(string[] args) { ClientContext context = new ClientContext("http://sp.weiyun.com/sites/Doc"); context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; FormsAuthenticationLoginInfo formsAuthinfo = new FormsAuthenticationLoginInfo("DEL00001", "1234!qwer"); context.FormsAuthenticationLoginInfo = formsAuthinfo; //context.Credentials = new NetworkCredential("DEL00001", "1234!qwer"); Web web = context.Web; List list = web.Lists.GetByTitle("DocLib"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @"<View> <Query> <Where> <Eq> <FieldRef Name = 'Title' /> <Value Type='Text'>71342245-c3c8-4094-a555-842a6763b201_081464afff6f457eac23e2168c82a974</Value> </Eq> </Where> </Query> </View>"; ListItemCollection items = list.GetItems(camlQuery); //context.Load(web,w=>w.Title,w=>w.Description); context.Load(items, s => s.Include(item => item["Title"])); //context.LoadQuery context.ExecuteQuery(); //string Title = web.Title; //Console.WriteLine(string.Format("Web Title is {0}, Descript is {1}!",Title,web.Description)); int i = items.Count; foreach (ListItem item in items) { Console.WriteLine("Title:{0}",item["Title"]); } }
private void LoginProjectServer() { Log.WriteVerbose(new SourceInfo(), "Logging into project server on url:{0}", TB_Url.Text); ProjContext = new ProjectContext(TB_Url.Text); Log.WriteVerbose(new SourceInfo(), "Authenticating against {0} pwa instance", CB_Online.Checked ? "Online" : "OnPerm"); if (CB_Online.Checked && RB_Forms.Checked) { //case online with user credential SecureString secpassword = new SecureString(); foreach (char c in TB_Password.Text) { secpassword.AppendChar(c); } ProjContext.Credentials = new SharePointOnlineCredentials(TB_UserName.Text, secpassword); } else if (!CB_Online.Checked && RB_Forms.Checked) { //case onprem with user credential ProjContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; FormsAuthenticationLoginInfo formsAuthInfo = new FormsAuthenticationLoginInfo(TB_UserName.Text, TB_Password.Text); ProjContext.FormsAuthenticationLoginInfo = formsAuthInfo; } else { //Default case - Windows Auth ProjContext.Credentials = CredentialCache.DefaultCredentials; } CsomHelper.ProjContext = ProjContext; CsomBase.CurrentResource = CsomHelper.LoadMe(); CsomBase.CurrentUser = CsomBase.CurrentResource.User; if (_bge.TaskCancelled || _bge.ActionTask.IsFaulted) { return; } Log.WriteVerbose(new SourceInfo(), "Login on url:{0} for user:{1}", TB_Url.Text, CsomBase.CurrentUser.Title); DialogResult = DialogResult.OK; }
internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, PSCredential credentials, PSHost host, bool currentCredentials, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool skipAdminCheck = false, ClientAuthenticationMode authenticationMode = ClientAuthenticationMode.Default) { var context = new PnPClientContext(url.AbsoluteUri); context.RetryCount = retryCount; context.Delay = retryWait * 1000; context.ApplicationName = Properties.Resources.ApplicationName; #if !ONPREMISES context.DisableReturnValueCache = true; #elif SP2016 context.DisableReturnValueCache = true; #endif context.RequestTimeout = requestTimeout; context.AuthenticationMode = authenticationMode; if (authenticationMode == ClientAuthenticationMode.FormsAuthentication) { var formsAuthInfo = new FormsAuthenticationLoginInfo(credentials.UserName, EncryptionUtility.ToInsecureString(credentials.Password)); context.FormsAuthenticationLoginInfo = formsAuthInfo; } if (!currentCredentials) { try { SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = onlineCredentials; try { context.ExecuteQueryRetry(); } catch (ClientRequestException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } catch (ServerException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } catch (ArgumentException) { // OnPrem? context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); try { context.ExecuteQueryRetry(); } catch (ClientRequestException ex) { throw new Exception("Error establishing a connection", ex); } catch (ServerException ex) { throw new Exception("Error establishing a connection", ex); } } } else { if (credentials != null) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } var connectionType = ConnectionType.OnPrem; if (url.Host.ToUpperInvariant().EndsWith("SHAREPOINT.COM")) { connectionType = ConnectionType.O365; } if (skipAdminCheck == false) { if (IsTenantAdminSite(context)) { connectionType = ConnectionType.TenantAdmin; } } var spoConnection = new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, credentials, url.ToString(), tenantAdminUrl, PnPPSVersionTag); spoConnection.ConnectionMethod = Model.ConnectionMethod.Credentials; return(spoConnection); }
internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, PSCredential credentials, PSHost host, bool currentCredentials, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool disableTelemetry, bool skipAdminCheck = false, ClientAuthenticationMode authenticationMode = ClientAuthenticationMode.Default) { var context = new PnPClientContext(url.AbsoluteUri); context.RetryCount = retryCount; context.Delay = retryWait * 1000; context.ApplicationName = Properties.Resources.ApplicationName; #if !ONPREMISES context.DisableReturnValueCache = true; #elif SP2016 || SP2019 context.DisableReturnValueCache = true; #endif context.RequestTimeout = requestTimeout; context.AuthenticationMode = authenticationMode; if (authenticationMode == ClientAuthenticationMode.FormsAuthentication) { var formsAuthInfo = new FormsAuthenticationLoginInfo(credentials.UserName, EncryptionUtility.ToInsecureString(credentials.Password)); context.FormsAuthenticationLoginInfo = formsAuthInfo; } if (!currentCredentials) { try { SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = onlineCredentials; try { context.ExecuteQueryRetry(); } #if !ONPREMISES catch (NotSupportedException nox) { #if NETSTANDARD2_1 // Legacy auth is not supported with .NET Standard throw nox; #else // legacy auth? var authManager = new OfficeDevPnP.Core.AuthenticationManager(); context = PnPClientContext.ConvertFrom(authManager.GetAzureADCredentialsContext(url.ToString(), credentials.UserName, credentials.Password)); context.ExecuteQueryRetry(); #endif } #endif catch (ClientRequestException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } catch (ServerException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } catch (ArgumentException) { // OnPrem? context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); try { context.ExecuteQueryRetry(); } catch (ClientRequestException ex) { throw new Exception("Error establishing a connection", ex); } catch (ServerException ex) { throw new Exception("Error establishing a connection", ex); } } } else { if (credentials != null) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } else { // If current credentials should be used, use the DefaultNetworkCredentials of the CredentialCache. This has the same effect // as using "UseDefaultCredentials" in a HttpClient. context.Credentials = CredentialCache.DefaultNetworkCredentials; } } #if SP2013 || SP2016 || SP2019 var connectionType = ConnectionType.OnPrem; #else var connectionType = ConnectionType.O365; #endif if (url.Host.ToUpperInvariant().EndsWith("SHAREPOINT.COM")) { connectionType = ConnectionType.O365; } if (skipAdminCheck == false) { if (IsTenantAdminSite(context)) { connectionType = ConnectionType.TenantAdmin; } } var spoConnection = new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, credentials, url.ToString(), tenantAdminUrl, PnPPSVersionTag, host, disableTelemetry, InitializationType.Credentials); spoConnection.ConnectionMethod = Model.ConnectionMethod.Credentials; return(spoConnection); }
internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, PSCredential credentials, PSHost host, bool currentCredentials, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool skipAdminCheck = false, ClientAuthenticationMode authenticationMode = ClientAuthenticationMode.Default) { var context = new PnPClientContext(url.AbsoluteUri); context.RetryCount = retryCount; context.Delay = retryWait * 1000; context.ApplicationName = Properties.Resources.ApplicationName; #if !ONPREMISES context.DisableReturnValueCache = true; #elif SP2016 context.DisableReturnValueCache = true; #endif context.RequestTimeout = requestTimeout; context.AuthenticationMode = authenticationMode; if (authenticationMode == ClientAuthenticationMode.FormsAuthentication) { var formsAuthInfo = new FormsAuthenticationLoginInfo(credentials.UserName, EncryptionUtility.ToInsecureString(credentials.Password)); context.FormsAuthenticationLoginInfo = formsAuthInfo; } if (!currentCredentials) { try { SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = onlineCredentials; try { context.ExecuteQueryRetry(); } catch (ClientRequestException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } catch (ServerException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } catch (ArgumentException) { // OnPrem? context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); try { context.ExecuteQueryRetry(); } catch (ClientRequestException ex) { throw new Exception("Error establishing a connection", ex); } catch (ServerException ex) { throw new Exception("Error establishing a connection", ex); } } } else { if (credentials != null) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } var connectionType = ConnectionType.OnPrem; if (url.Host.ToUpperInvariant().EndsWith("SHAREPOINT.COM")) { connectionType = ConnectionType.O365; } if (skipAdminCheck == false) { if (IsTenantAdminSite(context)) { connectionType = ConnectionType.TenantAdmin; } } return new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, credentials, url.ToString(), tenantAdminUrl, PnPPSVersionTag); }
private void LoginProjectServer() { Log.WriteVerbose(new SourceInfo(), "Logging into project server on url:{0}", TB_Url.Text); ProjContext = new ProjectContext(TB_Url.Text); Log.WriteVerbose(new SourceInfo(), "Authenticating against {0} pwa instance", CB_Online.Checked ? "Online" : "OnPerm"); if (CB_Online.Checked && RB_Forms.Checked) { //case online with user credential SecureString secpassword = new SecureString(); foreach (char c in TB_Password.Text) secpassword.AppendChar(c); ProjContext.Credentials = new SharePointOnlineCredentials(TB_UserName.Text, secpassword); } else if (!CB_Online.Checked && RB_Forms.Checked) { //case onprem with user credential ProjContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; FormsAuthenticationLoginInfo formsAuthInfo = new FormsAuthenticationLoginInfo(TB_UserName.Text, TB_Password.Text); ProjContext.FormsAuthenticationLoginInfo = formsAuthInfo; } else { //Default case - Windows Auth ProjContext.Credentials = CredentialCache.DefaultCredentials; } CsomHelper.ProjContext = ProjContext; CsomBase.CurrentResource = CsomHelper.LoadMe(); CsomBase.CurrentUser = CsomBase.CurrentResource.User; if (_bge.TaskCancelled || _bge.ActionTask.IsFaulted) { return; } Log.WriteVerbose(new SourceInfo(), "Login on url:{0} for user:{1}", TB_Url.Text, CsomBase.CurrentUser.Title); DialogResult = DialogResult.OK; }