2

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

Rana
  • 1,675
  • 3
  • 25
  • 51
  • You haven't provided any of the code you've tried so far. Are you looking for non-code solutions? In Windows 7, you can run an application using different Windows credentials than those you're logged in with by holding Shift and right-clicking on the executable, then selecting "Run as different user" – Thriggle Jan 13 '16 at 19:23
  • Hey, Thanks for your comment. I have included my code. Also, I am running windows server 2008 R2 Standard, not windows 7. – Rana Jan 13 '16 at 19:28

2 Answers2

2

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");
Thriggle
  • 7,009
  • 2
  • 26
  • 37
  • I tried that and now I get 401 (unauthorized error). I update my post above – Rana Jan 13 '16 at 20:16
  • Hi @TaimoorRana, the third parameter of the `NetworkCredential` constructor should be a string value containing the account's Active Directory domain prefix. For example, if your login is "MSFT\RanaTaimoor", the username is "RanaTaimoor" and the domain is "MSFT" – Thriggle Jan 13 '16 at 20:38
  • My username resembles as the following:ab12345 -(sorry I cannot share the exact username because it's a company account) – Rana Jan 13 '16 at 21:00
  • so "ab" would be my domain and "12345" will be my username. So I used: context.Credentials = new NetworkCredential("ab12345","password","http://sharepointsite/"); and I still get the same 401 error – Rana Jan 13 '16 at 21:06
  • 1
    Try instead `context.Credentials = new NetworkCredential("12345","password","ab")` – Thriggle Jan 13 '16 at 21:27
0

Don't know if it is late but, by default, the managed client object models authenticate users by using their Windows credentials (DefaultCredentials).

So you don't need to explicitly set the Credentials. Just Set following -

context.AuthenticationMode = ClientAuthenticationMode.Default;
Ram
  • 121
  • 3
  • 7