WinUIEx icon indicating copy to clipboard operation
WinUIEx copied to clipboard

Windows Maui app - WebAuthenticator for OIDC: activatedEventArgs.Kind is always Launch

Open rmaddu opened this issue 1 year ago • 9 comments

Hi, To resolve a bug in the Windows App SDK for OIDC in my Windows Maui app (Could not find file ..AppxManifest.xml), we are trying to integrate the fix provided in https://dotmorten.github.io/WinUIEx/concepts/WebAuthenticator.html https://dotmorten.github.io/WinUIEx/concepts/Maui.html#use-winuiexs-webauthenticator-instead-of-net-mauis

I have written code in App.xaml.cs: - public partial class App : MauiWinUIApplication {
public App() { if (WinUIEx.WebAuthenticator.CheckOAuthRedirectionActivation()) return; this.InitializeComponent();
}

call is triggering to CheckOAuthRedirectionActivation and checking activatedEventArgs.Kind whether it is a Protocol or not? My case always coming as Launch and returning False.

Also I have written below lines in identity service- WebAuthenticatorResult authResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(authUri, callbackuri);

I am gettng login page and able to enter credentials. But it is not returning. And in WebAuthenticator->Authenticate code, not getting the response from below line var uri = await tcs.Task.ConfigureAwait(false);

In manifest I have added -

$placeholder$

callbackuri is - {test://callback/}

Problem:

I am suspecting issue because of not getting the protocol kind, further call back is not registered initially, hence login page is not calling the call back function.

How to resolve this issue?

rmaddu avatar Jul 01 '24 10:07 rmaddu

@rmaddu did you also make the required changes to the windows app manifest?

dotMorten avatar Jul 02 '24 00:07 dotMorten

Yes. In ...\Platforms\Windows\Package.appxmanifest file I have added below lines. I didn't add any in ...\Platforms\Windows\app.manifest..

<Extensions> <uap:Extension Category="windows.protocol"> <uap:Protocol Name="test"> <uap:DisplayName>$placeholder$</uap:DisplayName> </uap:Protocol> </uap:Extension> </Extensions>

rmaddu avatar Jul 02 '24 04:07 rmaddu

Anything missing here. Why kind is Launch always and why login window is not calling the call back function?

Any help to resolve this?

rmaddu avatar Jul 02 '24 04:07 rmaddu

Hard to know. There's not a lot of info or a reproducer. Did you try debug into winuiex and see why? Does the Maui sample app work for you?

dotMorten avatar Jul 02 '24 04:07 dotMorten

1 2

sample snapshots added. Getting Launch always.

rmaddu avatar Jul 02 '24 05:07 rmaddu

One place says "test" and the other "myscheme".

Did you try the sample app that's in this repo? If that works try and reconfigure the app to work with your service and try again. Without a full reproducer app from you there's not much else I can offer than guesswork. The samples are there for you too to help debug.

dotMorten avatar Jul 02 '24 05:07 dotMorten

ok.

rmaddu avatar Jul 02 '24 05:07 rmaddu

Which sample should I execute - WinUIExMauiSample or WinUIExSample?

rmaddu avatar Jul 02 '24 05:07 rmaddu

Hi, I am able to debug the WinUIExSample. Workflow also success.

I have integrated the code to my application -

  • copied the WebAuthenticator.cs

  • Package.appxmanifest: added below lines <Extensions> <uap:Extension Category="windows.protocol"> <uap:Protocol Name="hsi.nwm"/> </uap:Extension> </Extensions>

  • in App.xaml.cs public App() { if (WebAuthenticator.CheckOAuthRedirectionActivation()) return; this.InitializeComponent(); }

  • in service code: var authResult = await WebAuthenticator.AuthenticateAsync(authUri, callbackuri);

    When I debug my app, I am getting login page. After successful login, asking to switch to app (see snapshot). But app code is not hitting.

1

any missing things here -

rmaddu avatar Jul 03 '24 10:07 rmaddu

Any help is appreciated...

rmaddu avatar Jul 04 '24 03:07 rmaddu

Please provide a sample app that reproduces the problem. I can't guess what you're missing. However since the sample app is working you're probably just missing something in your app or a setting is wrong.

dotMorten avatar Jul 04 '24 03:07 dotMorten

4 5 3**

Instead of using WinUiEx, copied the WebAuthenticator.cs to project. ** Do we need to copy this to any specific location in project or anywhere is fine? Do we need to copy any other files?

Also provide any links or threads so that I can cross check once again. As we have integrated the code in product, I can't share it.

do we need to update any in below -

6

rmaddu avatar Jul 04 '24 04:07 rmaddu

Please let me know if any other information is required?

rmaddu avatar Jul 04 '24 04:07 rmaddu

One observation: In WinUIEx sample: both redirect_uri and callbackUri is same. string callbackUri = "winuiex://"; string authorizeUri = $"{server.Url}?response_type={responseType}&client_id={clientId}&redirect_uri= {Uri.EscapeDataString(callbackUri)}&scope=photo+offline_access";

var result = await WebAuthenticator.AuthenticateAsync(new Uri(authorizeUri), new Uri(callbackUri), oauthCancellationSource.Token);

In my sample: both redirect uri and callbackUri are different. redirect_uri is HTTPS URL. string callbackUri = "hsi.nwm://callback/";
redirect_uri = https://XX.com/XXAuthenticatorService/signin-oidc

How to handle this situation?

Is it possible to use a custom protocol in the Package.appxmanifest?

rmaddu avatar Jul 04 '24 05:07 rmaddu

When I integrated my authurl (https://XX.com/XXAuthenticatorService/mobileauth/OpenIdConnect) in WinUIEx sample application, I am getting login page. After successful login, asking to switch to app. But app code is not hitting.

Seems redirecturi (i.e., HTTPS) and callbackuri ("winuiex://") are different, applications are not working.

Any help to resolve this?

rmaddu avatar Jul 04 '24 10:07 rmaddu

Any help is appreciated...

rmaddu avatar Jul 05 '24 02:07 rmaddu

The redirect url needs to use the same schema as what's in the app manifest and must be unique. You set that up in the service where you configure your app for oauth where you should also specify that schema.

dotMorten avatar Jul 05 '24 02:07 dotMorten

already specified (ServiceConfig.ini) - WebAuthenticator:CallbackScheme=hsi.nwm://callback

Package.appxmanifest: added below lines <Extensions> <uap:Extension Category="windows.protocol"> <uap:Protocol Name="hsi.nwm"/> </uap:Extension> </Extensions>

in service code:

string authUrl = "https://XX.com/XXService/mobileauth/OpenIdConnect"
Uri authUri = new Uri(authUrl);
Uri callbackuri = new Uri("hsi.nwm://callback/");
var authResult = await WebAuthenticator.AuthenticateAsync(authUri, callbackuri);

Getting below dialog, but not hitting app - image

Any missing thing here?

rmaddu avatar Jul 05 '24 11:07 rmaddu

Any help is appreciated...

rmaddu avatar Jul 05 '24 15:07 rmaddu

Asking over and over again for a response isn't helping. This is FREE software that I develop in my spare time. Please don't keep pinging me for updates over and over again and expect immediate responses for something I'm doing on the side for free without any expectation of pay. I've given you lots of hints and yet you still haven't provided the full sample that reproduces your issue like I asked. My time is limited so please provide something where I don't have to spend time to try and reproduce something with some unknown service. Especially if you're unwilling to spend some time debugging this issue using winuiex source yourself. Lots of people use this library for authentication so it's most likely a user error and not a bug so please refer to documentation and how oauth and app activation works first.

dotMorten avatar Jul 05 '24 15:07 dotMorten