microsoftgraph / msgraph-sdk-dotnet-auth

Archived - use the TokenCredential classes provided by Azure.Identity. https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme
https://graph.microsoft.com
MIT License
78 stars 19 forks source link

IntegratedWindowsAuthenticationProvider on .NET Core #56

Closed PhilPJL closed 4 years ago

PhilPJL commented 4 years ago

This code

var clientApplication = PublicClientApplicationBuilder
            .Create("<guid>")
            .WithTenantId("<guid>")
            .Build();
var authProvider = new IntegratedWindowsAuthenticationProvider(clientApplication);
var graphClient = new GraphServiceClient(authProvider);

works on .NET Framework, but fails on .NET Core with a PlatformNotSupportedException with message:

AcquireTokenByIntegratedWindowsAuth is not supported on .net core without adding .WithUsername() because MSAL cannot determine the username (UPN) of the currently logged in user. Please use .WithUsername() before calling ExecuteAsync(). For more details see https://aka.ms/msal-net-iwa

If I was using this directly:

var result = await clientApplication.AcquireTokenByIntegratedWindowsAuth(scopes).ExecuteAsync(); I could add WithUsername("") to ge:

var result = await clientApplication.AcquireTokenByIntegratedWindowsAuth(scopes).WithUsername("<username>").ExecuteAsync();

What's the solution when using the Auth library?

peombwa commented 4 years ago

Thanks for taking time to open this issue.

The Auth library has a WithUserAccount(GraphUserAccount) extension method that you can call on a request like this and set the users UPN to GraphUserAccount.Email property:

var clientApplication = PublicClientApplicationBuilder
    .Create("<guid>")
    .WithTenantId("<guid>")
    .Build();

var authProvider = new IntegratedWindowsAuthenticationProvider(clientApplication);

GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var me = await graphClient.Me.Request()
    .WithUserAccount(new GraphUserAccount { Email = "UPN" }) // username (UPN) 
    .GetAsync();
ghost commented 4 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.