Search code examples
c#microsoft-graph-apionedriveazure-ad-msalmicrosoft-graph-sdks

MSAL AcquireTokenWithDeviceCode Authentification on Clientside/Browser


i want to get access on OneDrive with Microsoft Graph SDK. I have a WebServer running on Azure. My Problem is that i activated 2Factor on microsoft acc and simple give username and password is not working anymore to login without user interaction with Msal. So i tried this:

           IPublicClientApplication app = PublicClientApplicationBuilder
                                              .Create(clientId)
                                              .Build();
            
            Func<DeviceCodeResult, Task> callback = item =>
            {
                return Task.FromResult(0);
            };

            authResult = app.AcquireTokenWithDeviceCode(scopes, callback)
                                        .ExecuteAsync()
                                        .Result;

            var authProvider = new DelegateAuthenticationProvider(async (request) =>
            {
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            });
            
            
            var graphClient = new GraphServiceClient(authProvider)

It stand still on "AcquireTokenWithDeviceCode" and will not doing something anymore. The only redirect uri is https://login.microsoftonline.com/common/oauth2/nativeclient with .NET Framework 4.6.1. I want the login window popup on the browser of the user, not on the azure webserver. How can i pass the login window to the browser of the user OR login without any user interaction with Msal?


Solution

  • You can use Get access on behalf of a user authorization method for logging in without the any user interaction.

    Please refer this document:https://learn.microsoft.com/en-us/graph/auth-v2-user