验证网络凭据以访问客户端对象模型上的SharePoint站点

时间:2014-07-24 08:54:55

标签: c# .net sharepoint-2010 sharepoint-2013 networkcredentials

我正在使用小应用程序,这需要将所有用户都放在给定站点的所有组中。我有两个网站; SharePoint 2010在线运行和SharePoint 2013联机运行。我收到凭据错误...

{"The remote server returned an error: (401) Unauthorized."}

 public class Program
{
    static void Main(string[] args)
    {

        string _siteURL = "https://intranet.co.uk";

        NetworkCredential _myCredentials = new NetworkCredential("user", "password", "https://intranet");

        ClientContext _clientContext = new ClientContext(_siteURL);

        _clientContext.Credentials = _myCredentials;

        Web _MyWebSite = _clientContext.Web;

        GroupCollection _GroupCollection = _MyWebSite.SiteGroups;

        _clientContext.Load(_GroupCollection);

        _clientContext.Load(_GroupCollection,
                             groups => groups.Include(group => group.Users)
             );

        _clientContext.ExecuteQuery();

        foreach (Group _group in _GroupCollection)
        {
            Console.WriteLine("Group Id   " + _group.Id + "     Group Title  " + _group.Title);

            UserCollection _userCollection = _group.Users;

            foreach (User _user in _userCollection)
            {
                Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}", _user.Title, _user.LoginName);
            }

            Console.WriteLine("\n.......................................");
        }

        Console.Read();

    }

1 个答案:

答案 0 :(得分:6)

您的凭据错误,特别是您的域名。

NetworkCredential _myCredentials = new NetworkCredential("user", "password", "https://intranet");

您的useraccount指定为domain\username,我怀疑您的域名是https://intranet,而是companyname。如果您不确定,请询问您的Exchange管理员。

您正在寻找的是类似的东西:

NetworkCredential _myCredentials = new NetworkCredential("user", "password", "companydomain");
//OR
NetworkCredential _myCredentials = new NetworkCredential(@"companydomain\user", "password");