Exemple #1
0
        private SPOAuthCookies GetSPOAuthCookies(SamlSecurityToken stsToken)
        {
            // signs in to SPO with the security token issued by MSO STS and gets the fed auth cookies
            // the fed auth cookie needs to be attached to all SPO REST services requests
            SPOAuthCookies spoAuthCookies = new SPOAuthCookies();

            Uri siteUri     = this.spSiteUrl;
            Uri wsSigninUrl = new Uri(String.Format("{0}://{1}/{2}", siteUri.Scheme, siteUri.Authority, spowssigninUri));

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(wsSigninUrl);

            request.CookieContainer = new CookieContainer();

            byte[] responseData = HttpHelper.SendHttpRequest(
                wsSigninUrl,
                "POST",
                stsToken.Token,
                "application/x-www-form-urlencoded",
                request,
                null).Response;

            if (request != null && responseData != null)
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    spoAuthCookies.FedAuth = (response.Cookies["FedAuth"] != null) ? response.Cookies["FedAuth"].Value : null;
                    spoAuthCookies.RtFA    = (response.Cookies["rtFA"] != null) ? response.Cookies["rtFA"].Value : null;
                    spoAuthCookies.Expires = stsToken.Expires;
                    spoAuthCookies.Host    = wsSigninUrl;
                }
            }

            return(spoAuthCookies);
        }
Exemple #2
0
        public CookieContainer GetCookieContainer()
        {
            if (stsAuthToken != null)
            {
                if (DateTime.Now > stsAuthToken.Expires)
                {
                    this.stsAuthToken = GetMsoStsSAMLToken();

                    // Check if we have a auth token
                    if (this.stsAuthToken != null)
                    {
                        SPOAuthCookies  cookies = GetSPOAuthCookies(this.stsAuthToken);
                        CookieContainer cc      = new CookieContainer();

                        Cookie samlAuthCookie = new Cookie("FedAuth", cookies.FedAuth)
                        {
                            Path     = "/",
                            Expires  = this.stsAuthToken.Expires,
                            Secure   = cookies.Host.Scheme.Equals("https"),
                            HttpOnly = true,
                            Domain   = cookies.Host.Host
                        };

                        cc.Add(this.spSiteUrl, samlAuthCookie);

                        Cookie rtFACookie = new Cookie("rtFA", cookies.RtFA)
                        {
                            Path     = "/",
                            Expires  = this.stsAuthToken.Expires,
                            Secure   = cookies.Host.Scheme.Equals("https"),
                            HttpOnly = true,
                            Domain   = cookies.Host.Host
                        };

                        cc.Add(this.spSiteUrl, rtFACookie);

                        this.cookieContainer = cc;
                    }
                }
            }

            return(this.cookieContainer);
        }