How do I pass user credentials from the console to SharePoint Online?

别说谁变了你拦得住时间么 提交于 2019-12-06 06:43:40

问题


I am trying to connect SharePoint 2013 Online website using a Context Token from a console executable. However, it is giving me error The remote server returned an error: (403) Forbidden.

Here is the code snippet:

string spurl = ConfigurationManager.AppSettings["Sharepoint_URL"].ToString();
using (ClientContext context = new ClientContext(spurl))
{
    context.Credentials = new NetworkCredential("username", "password", "domain");
    context.ExecuteQuery();
    Web web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
    Console.WriteLine(context.Web.Title);
 }               
 Console.ReadKey();

How can I make a connection with SharePoint Online? Does it only support claims-based authentication?


回答1:


I think the method with which you are trying to authenticate is outdated. The SharePoint 2013 Client Object Model now has a class called SharePointOnlineCredentials which abstracts away all the tedious cookie container stuff. E.g.:

using (ClientContext clientContext = new ClientContext("https://yoursite.sharepoint.com/"))  
{  
    SecureString passWord = new SecureString();
    clientContext.Credentials = new SharePointOnlineCredentials("loginname@yoursite.onmicrosoft.com", passWord);
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
    Console.WriteLine(web.Title);
    Console.ReadLine();
} 

Please refer to this link for more information: https://sharepoint.stackexchange.com/questions/83985/access-the-sharepoint-online-webservice-from-a-console-application



来源:https://stackoverflow.com/questions/22313548/how-do-i-pass-user-credentials-from-the-console-to-sharepoint-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!