示例#1
0
        public SPOService InstantiateSPOService(Uri url, string loginUrl, PSCredential credentials,
                                                string authenticationUrl = COMMON_AUTH_URL, PromptBehavior?behavior = null)
        {
            if (!IsValidServerVersion(url))
            {
                throw new InvalidOperationException(StringResourceManager.GetResourceString(
                                                        "ValidateServerVersionInvalidVersion",
                                                        new object[0]));
            }
            var context = new CmdLetContext(url.ToString(), null, null); // this is where the site "sites/Clients" is set.

            if (credentials == null)
            {
                OAuthSession session;
                if (CTX.SP1 == null)
                {
                    session = new OAuthSession(authenticationUrl);
                    session.SignIn(loginUrl, behavior.Value);                      // the login Url is the base site though.
                }
                else
                {
                    CTX.SP1.OAuthSession.EnsureValidAuthToken();
                    session = CTX.SP1.OAuthSession;
                }

                context.OAuthSession = session;
            }
            else
            {
                var credentials2 = new SharePointOnlineCredentials(credentials.UserName, credentials.Password);
                context.Credentials = credentials2;
            }
            return(new SPOService(context));
        }
示例#2
0
 internal bool IsValidServerVersion(Uri url)
 {
     using (var client = new WebClient())
     {
         client.Headers[HttpRequestHeader.UserAgent] = CmdLetContext.GetUserAgent();
         string str = null;
         try
         {
             client.DownloadData(url);
             str = client.ResponseHeaders[HEADER_SHAREPOINT_VERSION];
         }
         catch (WebException exception)
         {
             if ((exception != null) && (exception.Response != null))
             {
                 str = exception.Response.Headers[HEADER_SHAREPOINT_VERSION];
             }
         }
         if (str == null)
         {
             throw new InvalidOperationException(StringResourceManager.GetResourceString("ValidateServerVersionCouldntConnectToUri", new object[0]));
         }
         if (!string.IsNullOrEmpty(str))
         {
             string input = str.Split(new char[] { ',' })[0];
             if (Version.TryParse(input, out Version version))
             {
                 return(version.Major >= 15);
             }
         }
     }
     return(false);
 }
 public override PSCredential Read()
 {
     if (this.m_credential == null)
     {
         throw new InvalidOperationException(StringResourceManager.GetResourceString("CredentialCmdletPipebindReadNoPshost", new object[0]));
     }
     return(this.m_credential);
 }
 public static bool IsCredentialValid(PSCredential credential, PSHost host)
 {
     if (host == null)
     {
         throw new ArgumentNullException("host");
     }
     if ((credential == null) || ((credential.Password != null) && (credential.Password.Length != 0)))
     {
         return(true);
     }
     host.UI.WriteWarningLine(StringResourceManager.GetResourceString("AuthenticationHelperStrAuthenticateEmptyPassword", new object[0]));
     return(false);
 }
        public static PSCredential PromptForCredentials(PSHost host, string userName)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            PSCredential credential = null;

            do
            {
                credential = host.UI.PromptForCredential(StringResourceManager.GetResourceString("CmdletContextAuthenticationTitle", new object[0]), StringResourceManager.GetResourceString("CmdletContextAuthenticationMessage", new object[0]), (credential == null) ? userName : credential.UserName, string.Empty);
            }while (!IsCredentialValid(credential, host));
            return(credential);
        }