Esempio n. 1
0
        public static async Task <bool> Create(Uri spSiteUrl, string username, string password, bool useIntegratedWindowsAuth, bool verbose)
        {
            var             utility = new SpoAuthUtility(spSiteUrl, username, password, useIntegratedWindowsAuth, verbose);
            CookieContainer cc      = await utility.GetCookieContainer();

            var cookies = from Cookie c in cc.GetCookies(spSiteUrl) where c.Name == "FedAuth" select c;

            if (cookies.Count() > 0)
            {
                current = utility;
                return(true);
            }
            else
            {
                throw new Exception("Could not retrieve Auth cookies");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sends a JSON OData request appending the SharePoint canary to the request header.
        /// Appending the canary to the request is necessary to perform write operations (e.g. create, update, delete list items)
        /// The canary is a security measure to prevent cross site scripting attacks
        /// </summary>
        /// <param name="uri">The request uri</param>
        /// <param name="method">The http method</param>
        /// <param name="requestContent">A stream containing the request content</param>
        /// <param name="clientHandler">The request client handler</param>
        /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param>
        /// <returns></returns>
        public static async Task <byte[]> SendODataJsonRequestWithCanary(Uri uri, HttpMethod method, Stream requestContent, HttpClientHandler clientHandler, SpoAuthUtility authUtility, bool _verbose)
        {
            verbose = _verbose;
            // Make a post request to {siteUri}/_api/contextinfo to get the canary
            var response = await HttpUtility.SendODataJsonRequest(
                new Uri(String.Format("{0}/_api/contextinfo", SpoAuthUtility.Current.SiteUrl)),
                HttpMethod.Post,
                null,
                clientHandler,
                SpoAuthUtility.Current);

            var    serializer           = new JavaScriptSerializer();
            var    deserializedResponse = serializer.Deserialize <Dictionary <string, object> >(Encoding.UTF8.GetString(response, 0, response.Length));
            string canary = deserializedResponse["AuthURL"] as string;

            // Make the OData request passing the canary in the request headers
            return(await HttpUtility.SendODataJsonRequest(
                       uri,
                       method,
                       requestContent,
                       clientHandler,
                       SpoAuthUtility.Current,
                       new Dictionary <string, string> {
                { "X-RequestDigest", canary }
            }));
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a JSON OData request appending SPO auth cookies to the request header.
        /// </summary>
        /// <param name="uri">The request uri</param>
        /// <param name="method">The http method</param>
        /// <param name="requestContent">A stream containing the request content</param>
        /// <param name="clientHandler">The request client handler</param>
        /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param>
        /// <param name="headers">The http headers to append to the request</param>
        public static async Task <byte[]> SendODataJsonRequest(Uri uri, HttpMethod method, Stream requestContent, HttpClientHandler clientHandler, SpoAuthUtility authUtility, Dictionary <string, string> headers = null)
        {
            if (clientHandler.CookieContainer == null)
            {
                clientHandler.CookieContainer = new CookieContainer();
            }

            CookieContainer cookieContainer = await authUtility.GetCookieContainer(); // get the auth cookies from SPO after authenticating with Microsoft Online Services STS

            foreach (Cookie c in cookieContainer.GetCookies(uri))
            {
                clientHandler.CookieContainer.Add(uri, c); // apppend SPO auth cookies to the request
            }

            return(await SendHttpRequest(
                       uri,
                       method,
                       requestContent,
                       "application/json;odata=verbose;charset=utf-8", // the http content type for the JSON flavor of SP REST services
                       clientHandler,
                       headers));
        }
Esempio n. 4
0
 static async Task RunAsync(Uri sharepointUri, string username, string password, bool useIntegratedAuth, bool verbose)
 {
     await SpoAuthUtility.Create(sharepointUri, username, password, useIntegratedAuth, verbose);
 }