question

PrashantPatil-1644 avatar image
0 Votes"
PrashantPatil-1644 asked PrashantPatil-1644 answered

How to connect sharepoint onpremises site using azure AD user from C# Console program

We have migrated Users from ADFS to Azure AD. The following code is working for connecting ADFS users to the SharePoint site using the C# console program. I am able to log in to the SharePoint site from the browser with Azure AD user.

Can anyone suggest to me how to connect the Sharepoint on-premises site with AZURE AD user through the console program?





               ClientContext clientContext;

                 OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();

                 //Following code is to connect ADFS users to the SharePoint site.

                 clientContext = am.GetADFSUserNameMixedAuthenticatedContext(strSiteURL, strLoginUser, strLoginPassword,    strDomainName, strSTSName, strRealm);

                 FieldCollection fields = clientContext.Web.Fields;
                 IEnumerable<Field> results = clientContext.LoadQuery<Field>(fields.Where(item => item.Hidden != false));
                 clientContext.ExecuteQuery();


office-sharepoint-onlineoffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered

Hi @PrashantPatil-1644 ,

You can connect to SharePoint On-Premise with the Azure AD User using this function:

 private void _ConnectSPCSOM(string url, string user, SecureString password)
 {
     using (ClientContext context = new ClientContext(url))
     {
         context.AuthenticationMode = ClientAuthenticationMode.Default;
         context.Credentials = new System.Net.NetworkCredential(user, password);
         context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(_MixedAuthRequestMethod);
         try
         {
             Web web = context.Web;
             Console.WriteLine("Loading web...");
             context.Load(web);
             context.ExecuteQuery();
             Console.WriteLine(web.Title);
             Console.WriteLine(web.Url);
         }
         catch (Exception exception)
         {
             Console.WriteLine("Exception of type" + exception.GetType() + "caught.");
             Console.WriteLine(exception.Message);
             Console.WriteLine(exception.StackTrace);
         }
     }
 }
 private void _MixedAuthRequestMethod(object sender, WebRequestEventArgs e)
 {
     try
     {
         e.WebRequestExecutor.RequestHeaders.Remove("X-FORMS_BASED_AUTH_ACCEPTED");
         e.WebRequestExecutor.RequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
     }
     catch (Exception exception)
     {
         Console.WriteLine("Exception of type" + exception.GetType() + "caught.");
         Console.WriteLine(exception.Message);
         Console.WriteLine(exception.StackTrace);
     }
 }

Pass credential with System.Net.NetworkCredential.

Please refer the blog below for detailed information:

Using SharePoint CSOM with ADFS

Thanks
Best Regards



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PrashantPatil-1644 avatar image
0 Votes"
PrashantPatil-1644 answered

Hi @Jerryzy-MSFT
I have tried your code but it is giving me the following exception:

Exception of typeSystem.Net.WebExceptioncaught.
The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

Also, I have visited the blog which you mentioned. In that blog, they said that this cod is for connecting the SharePoint site with ADFS user.

Thanks for the suggestion.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.