If you have a SharePoint On Premise installation with a Custom Login Page , you might encounter issues working with CSOM. The error would be primarily related to Authentication Failure. The following solution worked for me.
Step 1: The Custom Login Page should be placed in IIS Virtual Directory.
As a general practise the custom login page is normally placed in the 15 Hive Layouts folder. Move/Copy the ASPX file of your custom login page from the 15 hive layouts folder to the following folder in IIS Virtual directory of the concerned Web Application:
Step 2: Edit the Custom Login page file to make sure the master page url is correct.
After the movement of the ASPX file, open the file in a notepad and check for the MasterPageFile value and change it to the following =”~/_layouts/15/simple.master“.
Step 3: Change the Custom Login Page Url in Central Administration.
Navigate to Central Admin and change the Url defined in Custom Login Page for the Web App ( Manage Web Application –> Select Web App –> Authentication Providers) to the following : “~/_forms/customlogin.aspx”
Step 4: For Mixed Mode Authentication Only.
If you are using Forms based authentication , you are already ready to go , however if you have mixed mode authentication then make sure to add the following event handler in the CSOM code :
ClientContext clientContext = new ClientContext(“http://servername/“);
clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(clientContext_ExecutingWebRequest);
Web site = clientContext.Web;
static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
CSOM should work fine now !!