3

We have to access a SharePoint site using FedAuth. We have the code which gets us the FedAuth cookie. What we are unable to get is how we would be using this FedAuth cookie in our ClientContext object.

Below is the code snippet.

static void Main(string[] args) {

 string siteUrl = "<SHAREPOINT SITE>";

 ClientContext clientContext = new ClientContext(siteUrl);
 clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
 //clientContext.AuthenticationMode = ClientAuthenticationMode.Anonymous;
 clientContext.Load(clientContext.Web);
 clientContext.RequestTimeout = 720000;
 clientContext.ExecuteQuery();

 Console.WriteLine("Name of the web is: " + clientContext.Web.Title);


}

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e) {
 e.WebRequestExecutor.WebRequest.CookieContainer = getCookieContainer(); // Get FedAuth cookie
 e.WebRequestExecutor.WebRequest.AllowAutoRedirect = true;
}

The method getCookieContainer() gets the FedAuthCookie. The request always times out (Web Exception has Caught. The operation has timed out) but no kind of authorization exception is thrown.

How can we access the SharePoint site with FedAuth cookie?

3
  • Are you on-premise? Which kind of authentication you are trying to achieve? ADFS with username\password or something else? – Sergei Sergeev Nov 23 '16 at 7:53
  • @Kai: On premise. We are using ADFS but for credentials we are sending default network credentials. – Naveen Nov 23 '16 at 8:35
  • @Naveen, I have exactly same objective as yours - using default network credential to perform ADFS auth to a sharepoint site. I would like to know how is your getCookieContainer() being implemented. Could you please post the code for completeness? Many thanks! – abbr Dec 27 '18 at 23:44
4

For that purpose I would strongly recommend you to use PnP AuthenticationManager class, which already contains methods for obtaining ClientContext with different authentication mechanism.
For example for your requirements there are

public ClientContext GetADFSUserNameMixedAuthenticatedContext
public ClientContext GetNetworkCredentialAuthenticatedContext

How to use:

  • Install PnP from nuget Install-Package SharePointPnPCore2013
  • Use any of the AuthenticationManager methods listed above

Each methods returns ClientContext bound to user.

Links:
authenticate-sharepoint-using-pnp-authentication-manager
Introduction-to-authentication-manager-in-pnp-core-library

1
  • Thanks for suggestion. We were able to fix the issue (see answer). – Naveen Nov 30 '16 at 7:44
1

We were finally able to fix the issue. The problem was that the method getCookieContainer() was asynchronously calling methods to get FedAuth cookie. So rather than writing like this:

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e) {
 e.WebRequestExecutor.WebRequest.CookieContainer = getCookieContainer(); // Get FedAuth cookie
 e.WebRequestExecutor.WebRequest.AllowAutoRedirect = true;
}

We called the method getCookieContainer() and stored it in an object and then assigned it in ExecutingWebRequest.

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e) {
 e.WebRequestExecutor.WebRequest.CookieContainer = cookieObj; // Store value returned from getCookieContainer() and then use it.
 e.WebRequestExecutor.WebRequest.AllowAutoRedirect = true;
}
1
ClientContext cc = new ClientContext(spSiteUrl);
cc.ExecutingWebRequest += (object sender, WebRequestEventArgs e) =>
{
    e.WebRequestExecutor.WebRequest.Headers[HttpRequestHeader.Cookie] = "FedAuth=" + _fedAuth + ";rtFa=" + _rtFa;
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.