private async Task <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 Uri siteUri = this.spSiteUrl; Uri wsSigninUrl = new Uri(String.Format("{0}://{1}/{2}", siteUri.Scheme, siteUri.Authority, spowssigninUri)); HttpClientHandler clientHandler; if (WebRequest.DefaultWebProxy.GetProxy(siteUri).ToString() != siteUri.ToString()) { if (verbose) { Console.WriteLine("Using proxy...[" + WebRequest.DefaultWebProxy.GetProxy(siteUri).ToString() + "]"); } clientHandler = new HttpClientHandler(); WebProxy proxy = new WebProxy((WebRequest.DefaultWebProxy.GetProxy(siteUri))); proxy.Credentials = CredentialCache.DefaultCredentials; proxy.UseDefaultCredentials = true; clientHandler.UseCookies = true; clientHandler.UseProxy = true; clientHandler.Proxy = proxy; } else { clientHandler = new HttpClientHandler(); } SPOAuthCookies spoAuthCookies = null; if (verbose) { Console.Write("Retrieving Cookies...."); } try { await HttpUtility.SendHttpRequest( wsSigninUrl, HttpMethod.Post, new MemoryStream(stsToken.Token), "application/x-www-form-urlencoded", clientHandler); spoAuthCookies = new SPOAuthCookies(); spoAuthCookies.FedAuth = clientHandler.CookieContainer.GetCookies(wsSigninUrl)["FedAuth"].Value; spoAuthCookies.RtFA = clientHandler.CookieContainer.GetCookies(wsSigninUrl)["rtFA"].Value; spoAuthCookies.Expires = stsToken.Expires; spoAuthCookies.Host = wsSigninUrl; if (verbose) { Console.WriteLine("[OK]"); } } catch (Exception e) { Console.WriteLine("[ERROR]:" + e.Message); throw new Exception("Problems retrieving Cookies."); } return(spoAuthCookies); }
public async Task <CookieContainer> GetCookieContainer() { if (stsAuthToken != null) { if (DateTime.Now > stsAuthToken.Expires) { this.stsAuthToken = await GetMsoStsSAMLToken(); SPOAuthCookies cookies = await 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); }
private async Task<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 Uri siteUri = this.spSiteUrl; Uri wsSigninUrl = new Uri(String.Format("{0}://{1}/{2}", siteUri.Scheme, siteUri.Authority, spowssigninUri)); HttpClientHandler clientHandler; if (WebRequest.DefaultWebProxy.GetProxy(siteUri).ToString() != siteUri.ToString()) { if (verbose) Console.WriteLine("Using proxy...[" + WebRequest.DefaultWebProxy.GetProxy(siteUri).ToString() + "]"); clientHandler = new HttpClientHandler(); WebProxy proxy = new WebProxy((WebRequest.DefaultWebProxy.GetProxy(siteUri))); proxy.Credentials = CredentialCache.DefaultCredentials; proxy.UseDefaultCredentials = true; clientHandler.UseCookies = true; clientHandler.UseProxy = true; clientHandler.Proxy = proxy; } else clientHandler = new HttpClientHandler(); SPOAuthCookies spoAuthCookies = null; if (verbose) Console.Write("Retrieving Cookies...."); try { await HttpUtility.SendHttpRequest( wsSigninUrl, HttpMethod.Post, new MemoryStream(stsToken.Token), "application/x-www-form-urlencoded", clientHandler); spoAuthCookies = new SPOAuthCookies(); spoAuthCookies.FedAuth = clientHandler.CookieContainer.GetCookies(wsSigninUrl)["FedAuth"].Value; spoAuthCookies.RtFA = clientHandler.CookieContainer.GetCookies(wsSigninUrl)["rtFA"].Value; spoAuthCookies.Expires = stsToken.Expires; spoAuthCookies.Host = wsSigninUrl; if (verbose) Console.WriteLine("[OK]"); } catch (Exception e) { Console.WriteLine("[ERROR]:"+e.Message); throw new Exception("Problems retrieving Cookies."); } return spoAuthCookies; }