Add Authentication to .NET MAUI Apps with Auth0

Thanks for the update,

I have gone through the changes, I will test it and update you.

1 Like

Thanks for the update,
I am able to configure the OKTA Auth serverId and it is working fine.
I have manually override the providerinformation of the OidcClient .
Sharing the code below, it may help full, if anybody else facing the same issue.

public async Task LoginAsync(CancellationToken cancellationToken = default)
{

    LoginRequest loginRequest = new LoginRequest
    {
        FrontChannelExtraParameters = new Parameters(new Dictionary<string, string>()
        {
            {"login_hint", "testuser@gmail.com"}
        })
    };

    string OKTAEndoint = Options.Domain + "/oauth2/" + Options.AuthServerId;
    oidcClient.Options.ProviderInformation = new ProviderInformation
    {
        IssuerName = OKTAEndoint,
        AuthorizeEndpoint = OKTAEndoint + "/v1/authorize",
        TokenEndpoint = OKTAEndoint + "/v1/token",
        EndSessionEndpoint = OKTAEndoint + "/v1/logout",
        UserInfoEndpoint = OKTAEndoint + "/v1/userinfo"
    };
    return await oidcClient.LoginAsync(loginRequest, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Thanks

1 Like

Thank you for sharing @sbkrishnan2506 :pray:

Hi,

If you want to force login every time please pass the Extra parameter along with Login_hint
LoginRequest request = new LoginRequest();
request.FrontChannelExtraParameters.Add(“login_hint”, email);
request.FrontChannelExtraParameters.Add(“prompt”, “login”);

1 Like

Hello!
With the help of this article, I was able to get auth working properly in my .NET MAUI app for Windows devices (I used the WebView work-around for the WebAuthenticator issue), but running on a Mac (Catalyst), when InvokeAsync is called, this line throws a TaskCanceledException immediately):

            WebAuthenticatorResult result = await WebAuthenticator.Default.AuthenticateAsync(

                new Uri(options.StartUrl),

                new Uri(options.EndUrl));

Any idea on what could cause that?

Thanks!
John

Hey there!

can you reference the article you are using? Thank you!

Oh, geeze. I thought I did that: Add Authentication to .NET MAUI Apps with Auth0

I even cloned the repo locally and got the same results on Mac.

I have got it to “work” (sort of) by specifying PrefersEphemeralWebBrowserSession = true as documented here. It pops open a new browser window without any saved credentials and allows me to log in. However, it’s not convient, as with the windows solution, it remembers credentials for a while, and the browser appears in-app. So the work-around isn’t ideal.

Thanks!

Okay then this thread will be perfect for that!

Hey @john.oehler,
I’m not able to reproduce your issue on my Mac. It works fine for me :thinking:
What is the message associated with the exception? Maybe it gives more details about the actual cause of the exception.
And I agree with you: the PrefersEphemeralWebBrowserSession workaround is not that convenient

Hey @andrea.chiarelli ,
The error happens in LoginAsync in Auth0Client.cs - the authorizeResult returned on line 75 is in error status, and the error is “UserCancel” with a null description.

All I did was click the login button and it appears that the discovery document and everything was loaded okay. Then a blank error message appears (because there error description is blank) in OnLoginClicked.

Any ideas of what could cause that?

Thanks!
John

Hey @john.oehler,
As I told you before, I cannot reproduce your error. However I have done some investigation based on your information.

First, the line you mentioned is not in the Auth0Client.cs file (which has only 61 lines :slightly_smiling_face:) but in the source code of the IdentityModel.OidcClient package. Specifically, in the OidcClient.cs file.

That said, by analyzing the source code and following the execution flow with the debugger, I found that the only case in which the UserCancel error occurs is when the user cancels the browser opening. In other words, when you click the Cancel button in this dialog:

image

Since you are using the WebAuthenticator on the Mac, the authentication happens in the external browser, so you have to click the “Continue” button in this dialog.

Honestly, I don’t see any other situation where the UserCancel error can occur :thinking:

Okay, thanks. I am not seeing that pop-up; but perhaps that’s because I’m currently renting a mac in the cloud, and I don’t have admin priveledges. I’ll test this out in another environment.

1 Like

Just to circle back on this - now that I have a local Mac to test on, my suspicion is confirmed; not having admin priveledges keeps the consent dialog from appearing at all, and it silently fails. Thanks for your help!

2 Likes

Hey @andrea.chiarelli,

I followed your blog post but am not having any luck with getting authentication to work. I cloned your repository, updated IdentityModel.OidcClient from 5.0.2 to 5.2.1 and receive the following error after calling LoginAsync(): Error redeeming code: Unauthorized / Unauthorized

Oddly enough, in the on manage.auth0.com → Monitoring → Logs I do see a “Success Login” message followed immediately by a “Failed Exchange”. I understand things with Maui have probably changed since the original posting. Any thoughts on this error?

Hey @stephenmmcdowell,
Welcome to the Auth0 Community!

Unfortunately, I’m not able to reproduce your error. I tested the app with OidcClient 5.2.1 and it worked without any problems.

The error message let me think of a configuration problem. Please, can you check if it’s configured correctly on the Auth0 side?
Also, have you tested the app as it comes from the repo, i.e., with OidcClient 5.0.2? Does it work?

If you still have problems, maybe a HAR file of your HTTP requests can help me understand what is happening (remove any sensitive data from it before passing it over)

Hey @andrea.chiarelli,
Thanks for getting back to me. I double checked Auth0 setup per the article and everything was as expected; the application type is ‘Native’ and both the callback and logout urls are set to ‘myapp://callback’. I had also ran the application as it came from the repo and did have the same problem. Attached is the HAR file.
MauiFailedLogin.har (26.9 KB)

Hey @stephenmmcdowell,
The HAR file you sent doesn’t contain the HTTP interaction between your application and the Auth0 server :thinking:

I had replaced my Auth0 domain with <MY_AUTH_DOMAIN>. Is that not the call?

There should be a sequence of calls, as outlined here. Here is a description of the flow that I should expect

Hey @andrea.chiarelli
I had a project where this error came up, so I cloned the github project to see if this error came up, and indeed. This is the error, and I don´t know where to look.
Exception: System.NullReferenceException: ‘Object reference not set to an instance of an object.’

Call stack: 0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at C:\Users\myUsername\AppData\Local\SourceServer\bb123c2c4d433ebca46498fd0d4b0728a9cd5eedc6e5d8398523e286da2078c9\JNINativeWrapper.g.cs:12,5 C#