Hi there,
My development environment is 1* SharePoint Server and 1* Laptop (the laptop doesn't join the SharePoint Domain). there is a SharePoint Site with SSL and Cert inside. Currently I try to connect to SharePoint Site by CSOM. My code like this:
I ran the source code in Server Side, everything work properly. Inversely, the code was ran in the client side, I got the error message like this "The remote server returned an error: (401) Unauthorized."
Is there any ways to sort it out?? Thanks
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext("https://xxx.dev.xxxxx.xxxxx"))
{
ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
bool validationResult = true;
return validationResult;
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(customXertificateValidation);
clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(context_ExecutingWebRequest);
clientContext.Credentials = new NetworkCredential("xxx", "xxx", "xxx");
// The SharePoint web at the URL.
Web web = clientContext.Web;
// We want to retrieve the web's properties.
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
Console.WriteLine(web.Title);
}
}
private static void context_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
//HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
//webReq.Proxy = new WebProxy("http://[ProxyAddress]"); //Specify a proxy address if you need to
//X509Certificate cert = X509Certificate.CreateFromCertFile(@"C:\oos.test.cmmp.hksarg.cer");
//webReq.ClientCertificates.Add(cert);
HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
X509Store userCaStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
string thumbprint = "09d7aeec71cd41d2190eac71906bb85faf285762";
thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
try
{
userCaStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificatesInStore = userCaStore.Certificates;
X509Certificate2Collection findResult = certificatesInStore.Find(X509FindType.FindByThumbprint, thumbprint, false);
X509Certificate2 clientCertificate = null;
if (findResult.Count == 1)
{
clientCertificate = findResult[0];
}
else
{
throw new Exception("Unable to locate the correct client certificate.");
}
webReq.ClientCertificates.Add(clientCertificate);
}
catch
{
throw;
}
finally
{
userCaStore.Close();
}
}
private static bool customXertificateValidation(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
return true;
}Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.