SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
When you say "hosted SharePoint", do you mean SharePoint Online (part of Office 365)? – Rob Windsor Feb 19 at 15:47
    
Yes. Sorry if that caused any confusion. – minnmass Feb 19 at 15:54
up vote 4 down vote accepted

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();
}
share|improve this answer
    
This did it; thank you! – minnmass Feb 19 at 15:58

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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