I'm new to Sharepoint generally, and need to write an app which will do some basic file management. First, though, it needs to log in.

The documentation I've found suggests that:

var clientContext = new ClientContext("https://company.sharepoint.com/");
clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(username, password);

Web web = clientContext.Web;

should connect to the server and get some basic information.

However, I'm getting an exception: "The user's login name or password is not valid.". I'm using my own credentials, with which I've logged into Sharepoint several times in a browser.

What am I doing wrong?

  • When you say "hosted SharePoint", do you mean SharePoint Online (part of Office 365)? Commented Feb 19, 2016 at 15:47
  • Yes. Sorry if that caused any confusion.
    – minnmass
    Commented Feb 19, 2016 at 15:54

2 Answers 2


When connecting to SharePoint Online, you should use SharePointOnlineCredentials instead of FormsAuthenticationLoginInfo.

using (var context = new ClientContext("https://contoso.sharepoint.com"))
    var password = new SecureString();
    foreach (var c in "Password")

    context.Credentials = new SharePointOnlineCredentials("[email protected]", password);

    var web = clientContext.Web;

You may want to check out this video from the Office 365 Developer Patterns and Practices Group. It enables you to achieve a similar result to using the SharePointOnlineCredentials without having to include your user name and password in the code.

Connecting to SharePoint Online from console application with Azure ADAL and PnP Core Component

Your Answer

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

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