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

2
  • 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" Commented Jan 13, 2016 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. Commented Jan 13, 2016 at 19:28

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

5 Comments

I tried that and now I get 401 (unauthorized error). I update my post above
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"
My username resembles as the following:ab12345 -(sorry I cannot share the exact username because it's a company account)
so "ab" would be my domain and "12345" will be my username. So I used: context.Credentials = new NetworkCredential("ab12345","password","sharepointsite/"); and I still get the same 401 error
Try instead context.Credentials = new NetworkCredential("12345","password","ab")
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.