public async Task <CookieContainer> GetCookieContainer() { if (stsAuthToken != null) { if (DateTime.Now > stsAuthToken.Expires) { stsAuthToken = await GetMsoStsSAMLToken(); if (stsAuthToken.Token != null) { SPOAuthCookies cookies = await GetSPOAuthCookies(stsAuthToken); CookieContainer cc = new CookieContainer(); Cookie samlAuthCookie = new Cookie("FedAuth", cookies.FedAuth) { Path = "/", Expires = stsAuthToken.Expires, Secure = cookies.Host.Scheme.Equals("https"), HttpOnly = true, Domain = cookies.Host.Host }; cc.Add(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(spSiteUrl, rtFACookie); 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 = spSiteUrl; Uri wsSigninUrl = new Uri(String.Format("{0}://{1}/{2}", siteUri.Scheme, siteUri.Authority, spowssigninUri)); var clientHandler = new HttpClientHandler(); await HttpUtility.SendHttpRequest( wsSigninUrl, HttpMethod.Post, new MemoryStream(stsToken.Token), "application/x-www-form-urlencoded", clientHandler); SPOAuthCookies 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; return(spoAuthCookies); }
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 = spSiteUrl; Uri wsSigninUrl = new Uri(String.Format("{0}://{1}/{2}", siteUri.Scheme, siteUri.Authority, spowssigninUri)); var clientHandler = new HttpClientHandler(); await HttpUtility.SendHttpRequest( wsSigninUrl, HttpMethod.Post, new MemoryStream(stsToken.Token), "application/x-www-form-urlencoded", clientHandler); SPOAuthCookies 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; return spoAuthCookies; }