0

I am implementing Xamarin AppAuth library with IdentityServer4. I cannot achieve redirect after authorization in custom tab back to the application. Authorization request is shown below:

AuthorizationRequest.Builder authReqBuilder = 
new AuthorizationRequest.Builder(authConfig,
                                 "clientId",
                                 ResponseTypeValues.Code,
                                 Uri.Parse("https://com.example/oauth2redirect"));
authReqBuilder.SetScope("api1 openid profile offline_access");

Here is configuration in AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          xmlns:tools="http://schemas.android.com/tools">
    <application ...>
        <activity
                android:name="net.openid.appauth.RedirectUriReceiverActivity"
                tools:node="replace">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https"
                      android:host="example.com"
                      android:path="/oauth2redirect" />
            </intent-filter>
        </activity>
    </application>
</manifest>

After I process the request and user log in, redirect url is applied in custom tab and the screen remains without redirect back to the native app.

Could you please give me some clues to make redirect back to the native app work?

Thanks in advance.

Update

GitHub repository: https://github.com/OldrichTodt/AppAuthExample/blob/master/README.md

Sign in code is called from Xamarin app in MainPage.xaml.cs Here is full code:

public async Task LoginAsync()
{
    try
    {
        var authConfig = await AuthorizationServiceConfiguration.FetchFromIssuerAsync(Uri.Parse("https://example.com"));

        AuthState authState = new AuthState(authConfig);

        AuthorizationRequest.Builder authReqBuilder = new AuthorizationRequest.Builder(authConfig,
                                                                                       "RealOneGame.Xamarin",
                                                                                       ResponseTypeValues.Code,
                                                                                       Uri.Parse("https://com.example/oauth2redirect"));
        authReqBuilder.SetScope("api1 openid profile offline_access");

        AuthorizationRequest authReq = authReqBuilder.Build();

        AuthorizationService authService = new AuthorizationService(Application.Context);

        authService.PerformAuthorizationRequest(authReq, PendingIntent.GetActivity(Application.Context, 0, new Intent(Application.Context, typeof(MyAuthCompleteActivity)), 0),
                                                         PendingIntent.GetActivity(Application.Context, 0, new Intent(Application.Context, typeof(MyAuthCanceledActivity)), 0));
    }
    catch (Exception ex)
    {
    }
}
  • I am switching to `IdentityModel.OidcClient` but I would appreciate any help to answer this question. – Oldřich Todt Dec 13 '18 at 07:37
  • Where do you call the sign-in code? In your xamarin app? If so, what do you mean redirect back to the native app? Could you share your project? – Billy Liu - MSFT Dec 13 '18 at 08:59
  • I'll provide some more info into question. I am not currently able, but wait please, I am going to upload later on. Do you want to upload IdentityServer4 project too? – Oldřich Todt Dec 13 '18 at 09:40
  • Activity `net.openid.appauth.RedirectUriReceiverActivity` is supposed to catch url in custom tab and process its query string. After user authorizes the page is redirected to https://com.example/oatuh2redirect but `MyAuthCompleteActivity` is not invoked. – Oldřich Todt Dec 13 '18 at 09:57
  • @BillyLiu-MSFT Added Xamarin code repository as you asked: [Repository](https://github.com/OldrichTodt/AppAuthExample/blob/master/README.md) – Oldřich Todt Dec 13 '18 at 11:50

1 Answers1

0

Could you please give me some clues to make redirect back to the native app work?

The custom tab is opened in your app, not start a new app. So the deep link you set will not work. You could set an onclick event in your authorization page's button or link to close the tab. For example:

<a href="#" onclick="window.close();">Click me to close</a>

Or you could refer to the following answer to close the custom tab: How to close chrome custom tabs

Billy Liu - MSFT
  • 2,168
  • 1
  • 8
  • 15
  • Thank you for the answer and sorry for late response. But if I close it with `window.close();`, I am not going to receive response. App Auth library need to catch redirect url which contains authorization code in query string. – Oldřich Todt Dec 18 '18 at 08:37