1

I have a site at SharePoint on-Premises which has both Form based authentication and windows authentication enabled. Users can login using either of them. Now, I try to use Client Object Model for an application. Here is the code:

ClientContext context = new ClientContext("http://mySPSite/sites/doc");

//context.Credentials = CredentialCache.DefaultNetworkCredentials;
context.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword", "myDomain");

Web site = context.Web;
context.Load(site);
context.ExecuteQuery();
Console.WriteLine("Title: {0}", site.Title);

However, no matter use default network credential or a valid credential, it shows this error:

{X-SharePointHealthScore: 0
SPRequestGuid: 45daf89c-2c2c-2007-4cbd-671a480d321e
request-id: 45daf89c-2c2c-2007-4cbd-671a480d321e
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 82
SPIisLatency: 0
X-Forms_Based_Auth_Required: http://******/_login/default.aspx?ReturnUrl=/_layouts/15/error.aspx&Source=%2fsites%2fdoc%2f_vti_bin%2fsites.asmx
X-Forms_Based_Auth_Return_Url: http://*****/_layouts/15/error.aspx
X-MSDAVEXT_Error: 917656; Access denied. Before opening files in this location, you must first browse to the web site and select the option to login automatically.
MicrosoftSharePointTeamServices: 15.0.0.4420
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Content-Length: 13
Cache-Control: private, max-age=0
Content-Type: text/plain; charset=utf-8
Date: Thu, 02 Apr 2015 18:49:30 GMT
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

}

Obviously, this is the issue:

X-MSDAVEXT_Error: 917656; Access denied. Before opening files in this location, you must first browse to the web site and select the option to login automatically.

Anyone knows how to fix this? Or, Maybe better NOT use SCOM? Use server Model?

Thanks

Add more info:

If go to http://mySPSite/sites/doc by browser, it shows below form. In the code, I want to use Windows or Form based authentication, as long as one works.

enter image description here

3

try this

context.Load(site);
context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
context.ExecuteQuery();

void ctx_MixedAuthRequest(object sender, WebRequestEventArgs e)
        {
            try
            {
                //Add the header that tells SharePoint to use Windows authentication.
                e.WebRequestExecutor.RequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
2

You need to add Client Authentication Mode before the Credentials to check with which credential user is going to log in into that URL.

ClientContext context = new ClientContext("http://mySPSite/sites/doc");

//This line is used for default Authentication    
context.AuthenticationMode = ClientAuthenticationMode.Default;
//This line is used for Form Based Authentication
context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;

//context.Credentials = CredentialCache.DefaultNetworkCredentials;
context.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword", "myDomain");

Web site = context.Web;
context.Load(site);
context.ExecuteQuery();
Console.WriteLine("Title: {0}", site.Title);
  • Thanks. It works for Form Authentication. However, it does not work for Windows authentication (I assume needs to set AuthenticationMode as Default), it shows the same error. How to make it work for Windows Authentication? BTW, if use form authentication, need set FormsAuthenticationLoginInfo instead of Credentials. – urlreader Apr 3 '15 at 15:57
0

Finally, I found this:

http://www.lifeonplanetgroove.com/accessing-mixed-mode-sharepoint-web-applications-with-csom-and-powershell/

https://msdn.microsoft.com/en-us/library/office/hh124553%28v=office.14%29.aspx

Basically, we need to use an event handler to add an HTTP Header to the request and force Windows Authentication. Otherwise, SP will redirect to the page where users have to select authentication mode.

For Form based authentication, it does not have this issue if AuthenticationMode is ClientAuthenticationMode.FormsAuthentication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.