locked
Get FedAuth cookie sharepoint site RRS feed

  • Question

  • User-595641371 posted

    I've been trying to get FedAuth cookie from SharePoint site.  I've written the following code to get cookies from HTTPWebResponse, but getting "The remote server returned an error: (403) Forbidden". I did googling and many of them asked me to add request.accept and request.credentials, but no luck.

            public Cookie GetAuthCookieWin()
            {            
                Cookie cookie = new Cookie();       
                                   
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HttpContext.Current.Request.Url.AbsoluteUri.ToString());
                // Provide a set of Windows credentials (default or explicit) 
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
                request.UseDefaultCredentials = true;
                request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    request.Method="GET"; request.Accept = "*/*"; // Assign the CookieContainer object CookieContainer cookies = new CookieContainer(); request.CookieContainer = cookies; request.AllowAutoRedirect = false; // Execute the HTTP request HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (null != response) { cookie = response.Cookies["FedAuth"]; } return cookie; }

    Could you please let me know the way of getting FedAuth cookie from site?

    Friday, February 20, 2015 4:57 AM

Answers

  • User-595641371 posted

    I found simple solution to get Fedauth with below line of code.

    HttpContext.Current.Request.Cookies["FedAuth"].Value.ToString()

    Thanks Krunal!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 25, 2015 12:19 PM

All replies

  • User1644755831 posted

    Hi whizkidgps,

    Have you tried this approach?

     public Cookie GetAuthCookie(String Url, String uname, String pswd)
            {
    
                CookieContainer CookieJar = new CookieContainer();
                Uri authServiceUri = new Uri(Url+ "/_vti_bin/authentication.asmx");
                HttpWebRequest spAuthReq = HttpWebRequest.Create(authServiceUri) as HttpWebRequest;
                spAuthReq.CookieContainer = CookieJar;
                spAuthReq.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/Login";
                spAuthReq.ContentType = "text/xml; charset=utf-8";
                spAuthReq.Method = "POST";
                string envelope =
                    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    + "<soap:Body>"
                    + "<Login xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">"
                    + "<username>{0}</username>"
                    + "<password>{1}</password>"
                    + "</Login>" + "</soap:Body>"
                    + "</soap:Envelope>";
                string userName = uname;
                string password = pswd;
                envelope = string.Format(envelope, userName, password);
                StreamWriter streamWriter = new StreamWriter(spAuthReq.GetRequestStream());
                streamWriter.Write(envelope);
                streamWriter.Close();
                HttpWebResponse response = spAuthReq.GetResponse() as HttpWebResponse;
                Cookie returnValue = response.Cookies[0];
                response.Close();
                return returnValue;
    
            }
    

    Please see below links. They might help you.

    http://hemant-vikram.blogspot.in/2012/01/programmatic-siteminder-20.html

    https://social.msdn.microsoft.com/Forums/sharepoint/en-US/00bb20a1-0e7f-4c3e-a1d5-9b8b08462bab/programmatically-getting-fedauth-cookie-from-sharepoint

    With Regards,

    Krunal Parekh

    Monday, February 23, 2015 1:27 AM
  • User-595641371 posted

    I found simple solution to get Fedauth with below line of code.

    HttpContext.Current.Request.Cookies["FedAuth"].Value.ToString()

    Thanks Krunal!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 25, 2015 12:19 PM