Add Authentication to .NET MAUI Apps with Auth0

Learn how to authenticate users of your .NET MAUI application using Auth0.
Read more…

:writing_hand:t2: Brought to you by @andrea.chiarelli

1 Like

What’s up Devs! How did you like this post? Please share any comments or feedback with us on this thread :thought_balloon:

1 Like

The tutorial is completely missing the part where you use the access token to call your API.

The access token in your tutorial does not work against API because the access token is missing payload because the IdentityModel library for some reason does not implement “audience” parameter at all, it’s not possible to specify audience in the OidcOptions.

1 Like

Fortunately the fix is easy. It would be great if you included an instruction that to call your API you have to send a request with audience set like this.

await oidcClient.LoginAsync(new LoginRequest()
{
    FrontChannelExtraParameters = new Parameters(new Dictionary<string,string>()
    {
        {"audience", "YourAPIIdentifier"}
    })
});
1 Like

Hey @miroslav.bartl,
Thank you for your feedback. Actually, the tutorial focused on authentication. For this reason the API call was not included

1 Like

Thanks for your suggestion! :raised_hands:

1 Like

This doesn’t work for me, double checked, everything is as it should be,

await auth0Client.LoginAsync();

never returns, login page never opens. Even tried

auth0Client.LoginAsync().Result;

it blocks.

Hi @cybercritics ,
Welcome to the Auth0 Community and thank you for trying the sample application.

As warned in the article, .NET MAUI is under constant improvement and the sample project worked with the implementation version available at that time. Things will be more predictable when the stable version will be released, presumably with .NET 7.

That said, did you try to download and run the final version published on this GitHub repo?
If you already did it, can you please tell me on which platform you experienced the issue (Mac, Windows, Android, everywhere)?
Thank you

1 Like

I gave up on it, was testing on Windows and Android emulator, will go back to it if I really need a login, seems you can do purchases now without any kind of client side authentication. Thanks for the quick reply.

I think the Windows issue with WebAuthenticator has been solved though, wasn’t getting any specific compile errors there.

1 Like

Thank you for letting me know about the resolution of the WebAuthenticator issue on Windows. I’ll give it a try and update the article as soon as possible

1 Like

I got it to work, is there a way to change the domain?

Glad to hear this! :tada:

What do you mean by “change the domain”? Are you talking about the Auth0 domain?

When you create a new tenant, you can choose the initial part of a domain (e.g., whatever-available.us.auth0.com), but after it’s created you cannot change it anymore.
Unless you want to use your own domain.

1 Like

Can’t get this to work either. On Windows, the login button is at least displayed, but on Android, you only seem to get a blank Home screen. Am using the latest updated version on GitHub, and have tried both with VS2022 and VS2022 Preview. Other MAUI apps seem to work fine in the same environment.

Hi @improwise,
Welcome to the Auth0 Community and thank you for reading the article.

.NET MAUI is constantly being improved and the sample project was tested with the framework version available at the time of writing. I explicitly mentioned in the article that issues could arise.
Please, consider this article a preview of .NET MAUI authentication with Auth0.
I’m committed to updating the article as soon as the stable version is released, presumably with .NET 7.

Was able to get this working by creating a new project on the latest VS template, then add your code, and finally modifying AndroidManifest.xml to

<uses-sdk android:targetSdkVersion="29" android:minSdkVersion="21" />

(default is 31 which requires some additional changes to work)

Thanks!

1 Like

Thank you for this update! :pray:

I can add that for anyone struggling with the AMM0000 Android Exported error in Visual Studio, it need to be added to the WebAuthenticationCallbackActivity file (or manifest I assume) like

[Activity(NoHistory = true, Exported = true, LaunchMode = LaunchMode.SingleTop)]

Lowering Android SKD will also work but perhaps not ideal for other reasons.

@andrea.chiarelli Thanks for a nice sample and article!

1 Like

Hi @andrea.chiarelli ,

I need to pass server id, discovery endpoint along with scopes, authority, client id… in OidcClientOptions. How can I do this? I don’t see any relevant property in OidcClientOptions to pass.
As a result, my access token is not properly generated, issuer, subject, audience are invalid.

Hi @gnanapriyasvmn ,
Welcome to the Auth0 Community! :wave:

I’d like to clarify that this article is focused on adding user authentication to the MAUI app and showing the user profile. It doesn’t include access token requests.
Bear in mind that the code shown in the article is just an example of how you can add authentication to a .NET MAUI application. It is not production-ready nor complete.

That said, I’m not sure to understand what you mean by “server id” and why you need the “discovery endpoint”.
If you need an access token, you should pass the audience (API identifier) to Auth0 through the OidcClient, as suggested here.

I’m currently working on an article about calling a protected API in a MAUI app, and hopefully it will be published next week. Please, wait until then to have directions on how to obtain an access token and how to call a secure API following best practices :pray:

2 Likes

Awesome @andrea.chiarelli!

Can’t wait for that article it’s exactly what I’m looking for, thanks for your hard work on this.

Boz.

1 Like