4

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;
clientContext.Load(web);
clientContext.ExecuteQuery();

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)? – Rob Windsor Feb 19 '16 at 15:47
  • Yes. Sorry if that caused any confusion. – minnmass Feb 19 '16 at 15:54
4

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")
    {
        secureString.AppendChar(c);
    }

    context.Credentials = new SharePointOnlineCredentials("login@contoso.onmicrosoft.com", password);

    var web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
}
0

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, privacy policy and cookie policy

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