How to authenticate Office 365 SharePoint Online with MFA enabled in c# using OfficeDevPnP.Core

32 Views Asked by At

I am using OfficeDevPnP.Core to authenticate Office 365 SharePoint Online with MFA enabled but I am getting error

 using (ClientContext ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAzureADCredentialsContext(siteUrl, username, securePassword))
        {
            // Your SharePoint Online operations go here
            // For example, retrieve the current user
            var currentUser = ctx.Web.CurrentUser;
            ctx.Load(currentUser);
            ctx.ExecuteQuery();

            Console.WriteLine($"Authenticated user: {currentUser.Title}");
        }

Value cannot be null.
Parameter name: value
   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at OfficeDevPnP.Core.AuthenticationManager.<AcquireTokenAsync>d__44.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OfficeDevPnP.Core.AuthenticationManager.<AcquireTokenAsync>d__43.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OfficeDevPnP.Core.AuthenticationManager.EnsureAzureADCredentialsToken(String resourceUri, String userPrincipalName, String userPassword, AzureEnvironment environment, KnownClientId clientId)
   at OfficeDevPnP.Core.AuthenticationManager.<>c__DisplayClass42_0.<GetAzureADCredentialsContext>b__0(Object sender, WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor()
   at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at Program.Main(String[] args) in C:\xxx\xxx\xxx\xxx\xxx\Program.cs:line 30

Any idea any one can help me out how I can Authenticate?

I have tried GetWebLoginClientContext I am able to authenticate but a pop up is coming every time i am uploading a file so i am trying alternative way to authenticate. So if any one can help me out in this using GetAzureADCredentialsContext

0

There are 0 best solutions below