【问题标题】:How to implement intent in android for AppAuth library?如何在 Android 中为 AppAuth 库实现意图?
【发布时间】:2018-12-12 16:56:45
【问题描述】:

我正在使用 IdentityServer4 实现 Xamarin AppAuth 库。在自定义选项卡中授权后,我无法实现重定向回应用程序。授权请求如下图:

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

这是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>

在我处理请求和用户登录后,重定向 url 应用在自定义选项卡中,屏幕保持不变,没有重定向回本机应用程序。

您能否给我一些线索以使重定向回本机应用程序正常工作?

提前致谢。

更新

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

从 MainPage.xaml.cs 中的 Xamarin 应用调用登录代码 这是完整的代码:

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)
    {
    }
}

【问题讨论】:

  • 我正在切换到IdentityModel.OidcClient,但如果能帮助我回答这个问题,我将不胜感激。
  • 你在哪里调用登录代码?在您的 xamarin 应用程序中?如果是这样,您是什么意思重定向回本机应用程序?你能分享你的项目吗?
  • 我将提供更多信息。我目前不能,但请稍候,我稍后会上传。你也想上传 IdentityServer4 项目吗?
  • Activity net.openid.appauth.RedirectUriReceiverActivity 应该在自定义选项卡中捕获 url 并处理其查询字符串。用户授权后页面重定向到com.example/oatuh2redirect,但MyAuthCompleteActivity没有被调用。
  • @BillyLiu-MSFT 按照您的要求添加了 Xamarin 代码存储库:Repository

标签: xamarin xamarin.android identityserver4 appauth


【解决方案1】:

您能否给我一些线索以使重定向回本机应用程序正常工作?

自定义选项卡在您的应用中打开,而不是启动新应用。所以你设置的深层链接将不起作用。您可以在授权页面的按钮或链接中设置onclick 事件以关闭选项卡。例如:

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

或者您可以参考以下答案来关闭自定义选项卡: How to close chrome custom tabs

【讨论】:

  • 感谢您的回答,对于迟到的回复感到抱歉。但如果我用window.close(); 关闭它,我将不会收到回复。 App Auth 库需要在查询字符串中捕获包含授权码的重定向 url。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多