private MsoCookies getSamlToken() { MsoCookies ret = new MsoCookies(); var sharepointSite = new { Wctx = office365Login, Wreply = _samlUrl + "_forms/default.aspx?wa=wsignin1.0" }; //get token from STS string stsResponse = getResponse(office365STS, sharepointSite.Wreply); // parse the token response XDocument doc = XDocument.Parse(stsResponse); // get the security token var crypt = from result in doc.Descendants() where result.Name == XName.Get("BinarySecurityToken", wsse) select result; // get the token expiration var expires = from result in doc.Descendants() where result.Name == XName.Get("Expires", wsu) select result; ret.Expires = Convert.ToDateTime(expires.First().Value); //generate response to Sharepoint HttpWebRequest sharepointRequest = HttpWebRequest.Create(sharepointSite.Wreply) as HttpWebRequest; sharepointRequest.Method = "POST"; sharepointRequest.ContentType = "application/x-www-form-urlencoded"; sharepointRequest.CookieContainer = new CookieContainer(); sharepointRequest.AllowAutoRedirect = false; // This is important byte[] data; using (Stream newStream = sharepointRequest.GetRequestStream()) { data = Encoding.UTF8.GetBytes(crypt.FirstOrDefault().Value); newStream.Write(data, 0, data.Length); newStream.Close(); using (HttpWebResponse webResponse = sharepointRequest.GetResponse() as HttpWebResponse) { ret.FedAuth = webResponse.Cookies["FedAuth"].Value; ret.rtFa = webResponse.Cookies["rtFa"].Value; } } return(ret); }
// Creates or loads cached cookie container CookieContainer getCookieContainer() { if (_cachedCookieContainer == null || DateTime.Now > _expires) { // Get the SAML tokens from SPO STS (via MSO STS) using fed auth passive approach MsoCookies cookies = getSamlToken(); if (cookies != null) { if (!string.IsNullOrEmpty(cookies.FedAuth)) { // Create cookie collection with the SAML token _expires = cookies.Expires; CookieContainer cc = new CookieContainer(); // Set the FedAuth cookie Cookie samlAuth = new Cookie("FedAuth", cookies.FedAuth) { Expires = cookies.Expires, Path = "/", Secure = cookies.Host.Scheme == "https", HttpOnly = true, Domain = cookies.Host.Host }; cc.Add(samlAuth); if (_useRtfa) { // Set the rtFA (sign-out) cookie, added march 2011 Cookie rtFa = new Cookie("rtFA", cookies.rtFa) { Expires = cookies.Expires, Path = "/", Secure = cookies.Host.Scheme == "https", HttpOnly = true, Domain = cookies.Host.Host }; cc.Add(rtFa); } _cachedCookieContainer = cc; return(cc); } } return(null); } return(_cachedCookieContainer); }
private MsoCookies getSamlToken() { MsoCookies ret = new MsoCookies(); try { var sharepointSite = new { Wctx = office365Login, //Wctx = "https://sso.hcl.com/adfs/ls/", Wreply = _host.GetLeftPart(UriPartial.Authority) + "/_forms/default.aspx?wa=wsignin1.0" //Wreply = "https://login.microsoftonline.com/login.srf?wa=wsignin1%2E0" }; //get token from STS string stsResponse = getResponse(office365STS, sharepointSite.Wreply); // parse the token response XDocument doc = XDocument.Parse(stsResponse); // get the security token var crypt = from result in doc.Descendants() where result.Name == XName.Get("BinarySecurityToken", wsse) select result; // get the token expiration var expires = from result in doc.Descendants() where result.Name == XName.Get("Expires", wsu) select result; ret.Expires = Convert.ToDateTime(expires.First().Value); HttpWebRequest request = createRequest(sharepointSite.Wreply); byte[] data = Encoding.UTF8.GetBytes(crypt.FirstOrDefault().Value); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); stream.Close(); using (HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse) { // Handle redirect, added may 2011 for P-subscriptions if (webResponse.StatusCode == HttpStatusCode.MovedPermanently) { HttpWebRequest request2 = createRequest(webResponse.Headers["Location"]); using (Stream stream2 = request2.GetRequestStream()) { stream2.Write(data, 0, data.Length); stream2.Close(); using (HttpWebResponse webResponse2 = request2.GetResponse() as HttpWebResponse) { ret.FedAuth = webResponse2.Cookies["FedAuth"].Value; ret.rtFa = webResponse2.Cookies["rtFa"].Value; ret.Host = request2.RequestUri; } } } else { ret.FedAuth = webResponse.Cookies["FedAuth"].Value; ret.rtFa = webResponse.Cookies["rtFa"].Value; ret.Host = request.RequestUri; } } } } catch (Exception) { return(null); } return(ret); }
MsoCookies getSamlToken() { MsoCookies ret = new MsoCookies(); var sharepointSite = new { Wctx = office365Login, Wreply = _samlUrl + "_forms/default.aspx?wa=wsignin1.0" }; //get token from STS string stsResponse = getResponse(office365STS, sharepointSite.Wreply); // parse the token response XDocument doc = XDocument.Parse(stsResponse); // get the security token var crypt = from result in doc.Descendants() where result.Name == XName.Get("BinarySecurityToken", wsse) select result; // get the token expiration var expires = from result in doc.Descendants() where result.Name == XName.Get("Expires", wsu) select result; ret.Expires = Convert.ToDateTime(expires.First().Value); //generate response to Sharepoint HttpWebRequest sharepointRequest = HttpWebRequest.Create(sharepointSite.Wreply) as HttpWebRequest; sharepointRequest.Method = "POST"; sharepointRequest.ContentType = "application/x-www-form-urlencoded"; sharepointRequest.CookieContainer = new CookieContainer(); sharepointRequest.AllowAutoRedirect = false; // This is important byte[] data; using (Stream newStream = sharepointRequest.GetRequestStream()) { data = Encoding.UTF8.GetBytes(crypt.FirstOrDefault().Value); newStream.Write(data, 0, data.Length); newStream.Close(); using (HttpWebResponse webResponse = sharepointRequest.GetResponse() as HttpWebResponse) { ret.FedAuth = webResponse.Cookies["FedAuth"].Value; ret.rtFa = webResponse.Cookies["rtFa"].Value; } } return ret; }
private MsoCookies getSamlToken() { MsoCookies ret = new MsoCookies(); try { var sharepointSite = new { Wctx = office365Login, Wreply = _host.GetLeftPart(UriPartial.Authority) + "/_forms/default.aspx?wa=wsignin1.0" }; //get token from STS string stsResponse = getResponse(office365STS, sharepointSite.Wreply); // parse the token response XDocument doc = XDocument.Parse(stsResponse); // get the security token var crypt = from result in doc.Descendants() where result.Name == XName.Get("BinarySecurityToken", wsse) select result; // get the token expiration var expires = from result in doc.Descendants() where result.Name == XName.Get("Expires", wsu) select result; ret.Expires = Convert.ToDateTime(expires.First().Value); HttpWebRequest request = createRequest(sharepointSite.Wreply); byte[] data = Encoding.UTF8.GetBytes(crypt.FirstOrDefault().Value); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); stream.Close(); using (HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse) { // Handle redirect, added may 2011 for P-subscriptions if (webResponse.StatusCode == HttpStatusCode.MovedPermanently) { HttpWebRequest request2 = createRequest(webResponse.Headers["Location"]); using (Stream stream2 = request2.GetRequestStream()) { stream2.Write(data, 0, data.Length); stream2.Close(); using (HttpWebResponse webResponse2 = request2.GetResponse() as HttpWebResponse) { ret.FedAuth = webResponse2.Cookies["FedAuth"].Value; ret.rtFa = webResponse2.Cookies["rtFa"].Value; ret.Host = request2.RequestUri; } } } else { ret.FedAuth = webResponse.Cookies["FedAuth"].Value; ret.rtFa = webResponse.Cookies["rtFa"].Value; ret.Host = request.RequestUri; } } } } catch (Exception) { return null; } return ret; }