Authenticate network credential to access SharePoint site on client object model

24,155

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");
Share:
24,155
K.Z
Author by

K.Z

Updated on July 24, 2020

Comments

  • K.Z
    K.Z almost 4 years

    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();
    
        }
    
  • K.Z
    K.Z almost 10 years
    i have feelings that i am doing something wrong on credentials details ... i try different options and come bk... many thanks in advance for helping me
  • K.Z
    K.Z almost 10 years
    does it matter that SharePoint 2010 is running on premisses ... can i use same approach as you have define????????
  • Marco
    Marco almost 10 years
    Your code is fine. The only problem you have is, that you used the wrong value for the domain in Credentials. If you can reach your _siteUrl in your browser, you can also reach it in code.
  • K.Z
    K.Z almost 10 years
    does it seems correct??? NetworkCredential _myCredentials = new NetworkCredential(@"dir\myUserName", "xyz", "intranet.xyz.ac.uk");
  • Marco
    Marco almost 10 years
    No. if dir is your domain it is either 1. "dir\myUserName", "xyz" OR: 2. "myUserName", "xyz", "dir". Ask your administrators what your domain is, then use what they tell you and enter it as your domain
  • K.Z
    K.Z almost 10 years
    I have use option 1 with dir\myUserName", "xyz and it works .... many thanks once again for helping me