如何将用户凭据从控制台传递到SharePoint Online?

时间:2014-03-10 23:32:29

标签: sharepoint sharepoint-online

我正在尝试使用控制台可执行文件中的上下文令牌连接SharePoint 2013 Online网站。但是,它给了我错误The remote server returned an error: (403) Forbidden.

以下是代码段:

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();

如何与SharePoint Online建立连接?它是否仅支持基于声明的身份验证?

1 个答案:

答案 0 :(得分:6)

我认为您尝试进行身份验证的方法已过时。 SharePoint 2013客户端对象模型现在有一个名为SharePointOnlineCredentials的类,它抽象出所有繁琐的cookie容器内容。 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();
} 

有关详细信息,请参阅此链接:https://sharepoint.stackexchange.com/questions/83985/access-the-sharepoint-online-webservice-from-a-console-application