Regarding "OfficeDevPnP.Core.AuthenticationManager" Connect ot HTTPS SharePoint Site

Brass Contributor

Hi there,

 

I connect to SharePoint OnPrem by OfficeDevPnP.Core.AuthenticationManager. It turns out that "{"The remote server returned an error: (401) Unauthorized."}". I list out my sample code like the shown below, does anyone have any idea on it? Thanks

 

try
            {
                SecureString secureStringPassword = GetSecureString(password);

                //Get instance of Authentication Manager  
                OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
                var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(siteUrl, farmAdmin, secureStringPassword, domain);

                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                clientContext.Load(clientContext.Web);
                clientContext.Load(clientContext.Site);
                clientContext.Load(clientContext.Site.RootWeb);
                clientContext.ExecuteQuery();

                return clientContext;
            }
            catch (Exception eX)
            {
                throw eX;
            }

 

private static SecureString GetSecureString(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");

            var securePassword = new SecureString();

            foreach (char c in password)
                securePassword.AppendChar(c);

            securePassword.MakeReadOnly();

            return securePassword;
        }

 

0 Replies