Search code examples
c#authenticationsharepointntlmsharepoint-clientobject

Sharepoint 2010 User Authentication (windows Credential) with Client Object Model


I am trying to login to a SharePoint website that uses Windows Integrated (NTLM) authentication. There are 2 ways to enter credentials for the SharePoint website, Windows Authentication and form authentication.

However, Form authentication is disable on this specific website and I can only use windows authentication. Is there a way for me to login to this site with different credential than what I used to login to my windows machine?

See error here: Form authentication denied

        String site = "http://sharepoint/";
        ClientContext context = new ClientContext(site);
        context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
        FormsAuthenticationLoginInfo formsAuthInfo = new FormsAuthenticationLoginInfo("MyUser", "MyPassword");
        context.FormsAuthenticationLoginInfo = formsAuthInfo;

        // The SharePoint web at the URL.
        Web web = context.Web;

        // We want to retrieve the web's properties.
        context.Load(web);

        // Execute the query to the server.
        context.ExecuteQuery();


        InitializeComponent();

I also tried to use: context.Credentials = new NetworkCredential("user", "pass", site);

       ClientContext context = new ClientContext(site);
       context.Credentials = new NetworkCredential("user", "pass", site);


        // The SharePoint web at the URL.
        Web web = context.Web;

        // We want to retrieve the web's properties.
        context.Load(web);

        // Execute the query to the server.
        context.ExecuteQuery();


        InitializeComponent();

I get the following 401 (unauthorized) error


Solution

  • Instead of changing the ClientContext object's AuthenticationMode property to FormsAuthentication, try setting the object's Credentials property to a valid Network Credential object.

    ClientContext context = new ClientContext("http://sharepointsite/");
    context.Credentials = new NetworkCredential("username","password","domain");