asp.net-coremauiazure-ad-b2c

How to authenticate a user that was redirected to a web application from a desktop application


I am having an issue trying to use Azure AD B2C to use SSO between my desktop application and my web application. The issues is almost exactly what is described in this question: Microsoft Question

I have tried following the steps in that answer but I am having issues trying to implement steps 5 and 6. My hope is that someone might be able to point me to a code sample of where I can pass in the access token to my Web Application and use the access token to authenticate the user without having them sign in again.

I have the User Flows session behavior set to use the tenant option for SSO. Currently I have both applications set to use the same user flow. They do use different App Registrations though since the setup for a Desktop applications is different from the App Registration settings for my Web Application.


Solution

  • Not sure about how Azure works but you can use the following to authenticate using a web browser and redirect to the app and use the token in there.

    In MAUI you have an interface called WebAuthenticator https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/communication/authentication?view=net-maui-8.0&tabs=windows which works well for android and iOS but unfortunately doesn't work for windows.

    As a replacement you can use the following:

    Just copy the file in your Platforms -> windows -> WebAuthenticator.cs and you use it as is. It replicates the APIs already in place

    
    try
    {
        WebAuthenticatorResult authResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(
            new Uri("https://yoururl"),
            new Uri("myapp://"));
    
        string accessToken = authResult?.AccessToken;
    
        // Do something with the token
    }
    catch (TaskCanceledException e)
    {
        // Use stopped auth
    }
    
    

    This is assuming that you have setup your windows callback url in your manifest file.