Call a Protected API from a .NET MAUI App

Hello, i used the blog and have the app able to run on my windows desktop as expected.

but when i try to run it on my android phone it stops with the login. the browser login page is shown and i enter the username and password but it never completes. i am trying to figure out what to do at this point.
i am not seeing any error message.

ran the same code on the same phone but with no debug and it worked!! what the heck!!

Hey @figuerres,
I’m not a mobile developer, so I’m not sure if I can effectively help you.
I need some information to try to understand what can happen.

…i enter the username and password but it never completes.

What do you mean by it never completes? Do you get stuck on the login page? Are you redirected to the app but you are still not authenticated?

In the first case, can you track the HTTP interactions of the browser to see if the authentication process takes place?

Actually, the fact that it doesn’t work with debug but works without debug looks weird :thinking:

i am learning to use maui right now so i am also a bit at a loss.

when i run the maui app on windows the browser window pops up and i can login and the app runs.
if i try to use the android emulator on windows i see the app start, i click the login button and then i see it try to run chrome in the emulator and then the emulator crashes.
if i try to use my actual phone with debug it at first was just showing the login ui page but it never returns back to the application. also auth0 was not logging that attempt at a login.

if i run the app on my phone without debug that works.

i am guessing that this must all have something to do with android permissions and or the browser on android.

for now i am going to work to mockup the login and be able to run code with a fake login till i get farther in the application and switch running it each way…

also a thing i want to find out that perhaps you can look at is how to make the login be a part of the app startup.

the app i am creating will need to ONLY work if the user has logged in. it will be doing order entry and should not let non-authenticated users access any functions.
so ideally the app never gives the user any options untill they login…
i think i can do this a few ways but i am not sure what the best one is.

You can take a look at this thread to see if your Android problems are similar to someone’s else there.

Regarding, your request about launching the login at startup, I think you can try using page events, but I can’t say if it works or is a best practice for MAUI.

i found this video to be a best way to setup login

the author has a large number of very good videos on xamarin and maui app building.

a few more notes for the updates to the blogs / tutorials:

i think that several of the downloadable maui demo apps have the same or nearly the same project names and the also seem to use the same sample redirect uri of myapp://callback/

IF a developer downloads and runs more than one of them and leaves the uri the same then windows may not know which maui app to run and may ask the user which one to open…

if that starts to happen it may keep going to the wrong app.

  1. name the demo apps differently for each blog post.
  2. give each demo app a different callback uri prefix / scheme name like demo1:// demo2:// and so on.
  3. add a note to the blog that if you have multiple maui applications that the call back needs to have a unique name for each app or this can and will happen.

took me a few minutes to figure this out…

Hey @figuerres,
Thank you so much for your suggestions to improve our content :pray: I really appreciate your feedback.

Related to the application names and callback URIs (but this also applies to audiences and any other parameter that is specific to an application), keep in mind that the ones proposed in the articles are like placeholders. You need to customize them based on your needs, environment, preferences, etc.
For example, for the callback URI for a native application, the article proposes myapp://callback, but you should define yours also based on the best practices for your operating system. See here for some context.

here is another bit of info:
i want to get Swashbuckle to work with Auth0

i did find this blog that says they did is but they add stuff that i think makes it harder to setup:

when i get my web api working i will see if i can post a simple version…

the link has added code that while it might be useful it adds i think to many things that are not core to just getting the ui to work.
i did not see an Auth0 blog post for this…

basically we need the swagger ui to get an auth0 token so that it can pass the access token to the api controllers.

Hey @figuerres,
I did something similar to what you shared (but a little bit simpler, I think) some time ago here. However, this is not what you are looking for, I believe.

Implementing a Swagger UI that gets an access token requires anyway user authentication. So, if you don’t get it manually from the Auth0 dashboard, you need to implement a client that gets it on you behalf, i.e., after authentication.

I think it’s doable, but we don’t have any sample showing it currently.

When debugging, I’m getting the following error when my application opens Chrome to start authentication process with Auth0. Has anyone experienced the same?

EXCEPTION: Mono.Debugging.Soft.DisconnectedException: The connection with the debugger has been lost. The target application may have exited. —> Mono.Debugger.Soft.VMDisconnectedException: Exception of type ‘Mono.Debugger.Soft.VMDisconnectedException’ was thrown.

at Mono.Debugger.Soft.Connection.disconnected_check() in D:\a_work\1\s\External\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\Connection.cs:line 1573

at Mono.Debugger.Soft.Connection.SendReceive(CommandSet command_set, Int32 command, PacketWriter packet) in D:\a_work\1\s\External\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\Connection.cs:line 1900

at Mono.Debugger.Soft.Connection.EnableEvent(EventType etype, SuspendPolicy suspend_policy, List`1 mods) in D:\a_work\1\s\External\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\Connection.cs:line 2842

at Mono.Debugger.Soft.EventRequest.SendReq(List`1 mods) in D:\a_work\1\s\External\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\EventRequest.cs:line 106

at Mono.Debugging.Soft.SoftDebuggerSession.OnEnableBreakEvent(BreakEventInfo eventInfo, Boolean enable) in D:\a_work\1\s\External\debugger-libs\Mono.Debugging.Soft\SoftDebuggerSession.cs:line 1372

at Mono.Debugging.Client.DebuggerSession.UpdateBreakEventStatus(BreakEvent be) in D:\a_work\1\s\External\debugger-libs\Mono.Debugging\Mono.Debugging.Client\DebuggerSession.cs:line 783

Hello @ibicom,
I’m not sure I can help you with this error. Looking around, it looks like a generic error from the debugger. To know the actual error message and learn the reason of the failure, you should go through the logs.
This depends on the platform you are using, of course.
See this answer on Stack Overflow, for example.

Hi Andrea,
Thanks for replying.
Meanwhile I was able to fix it.
The problem was not library related at all, but inability for emulator to open the Chrome, even when I tried starting Chrome manually.
The solution was to use emulator with API 31.

1 Like

Cool! Thank you for sharing, @ibicom