Authenticate network credential to access SharePoint site on client object model

点点圈 提交于 2020-02-02 06:22:34

问题


I am working to small app, that is require to bring all users in all groups of given site. I have two sites; SharePoint 2010 running on premisses and SharePoint 2013 online. I am getting credential error...

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

code.

 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:


Your credentials are wrong, espacially your domain.

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

Your useraccount is specified as domain\username and I doubt your domainname would be https://intranet, but rather companyname. If you are not sure, ask your Exchange administrator.

What you are looking for is something analog to this:

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


来源:https://stackoverflow.com/questions/24929259/authenticate-network-credential-to-access-sharepoint-site-on-client-object-model

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