3

我正在开发小型应用程序,这需要将所有用户带到给定站点的所有组中。我有两个站点;本地运行的 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();

    }
4

1 回答 1

6

您的凭据错误,尤其是您的域。

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

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

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

NetworkCredential _myCredentials = new NetworkCredential("user", "password", "companydomain");
//OR
NetworkCredential _myCredentials = new NetworkCredential(@"companydomain\user", "password");
于 2014-07-24T09:32:56.503 回答