gpt4 book ai didi

android - Xamarin.Auth (Android) - Chrome 自定义选项卡不会在重定向时关闭

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:50 25 4
gpt4 key购买 nike

我已经实现了 Xamarin.Auth sample code在 Android 上使用谷歌的身份提供者进行身份验证。我已使用设备的 Chrome 浏览器成功导航到 Google 登录页面,我可以在其中输入我的凭据。我成功地向谷歌授权,但当它重定向回我的应用程序时,Chrome 自定义选项卡没有关闭,即,我只剩下在 chrome 浏览器中查看谷歌搜索。如果我关闭浏览器,我可以再次看到我的应用程序,其中显示了从 google 身份提供商返回的用户详细信息。

为什么 Chrome 的自定义选项卡在来自 Google 身份提供商的重定向时不关闭,我如何使用 Xamarin Forms 和 Xamarin.Auth 关闭它?

最佳答案

如果在 Android Xamarin.Auth 示例中捕获重定向 (CustomUrlSchemeInterceptorActivity) 的类中的 OnCreate 方法末尾添加此代码,则可以返回到您的应用

new Task(() =>{
StartActivity(new Intent(Application.Context,typeof(MainActivity)));
}).Start();

其中 MainActivity 是您在 Android 中的主 Activity 类的名称。更准确地说,这是一个完整的类,您可以为拦截的每个重定向继承该类

public class UrlSchemeInterceptor : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
base.OnCreate(savedInstanceState);

// Convert Android.Net.Url to Uri
var uri = new Uri(Intent.Data.ToString());
new Task(() =>
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
intent.AddFlags(ActivityFlags.ReorderToFront);
StartActivity(intent);

}).Start();

// Load redirectUrl page
AuthenticationState.Authenticator.OnPageLoading(uri);
Finish();

}
catch (Exception e)
{
Console.WriteLine(e);
}
}

}

public class AuthenticationState
{
public static WebAuthenticator Authenticator;
/*This static field is used to store the object
from OAuth1Authenticator or OAuth2Authenticator
upon initialization in the UI (Xamarin forms or Android or iOS).
For example:
var authenticatorObject = new OAuth2Authenticator (YOUR PARAMETERS);
AuthenticationState.Authenticator = (WebAuthenticator)authenticatorObject;
var presenter = new OAuthLoginPresenter();
presenter.Login(authenticatorObject);
*/
}

例如谷歌案例

[Activity(Label = "YOURLABEL", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter( new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[]
{
"com.googleusercontent.apps.",// YOUR GOOGLE ID INVERTED
},
DataPaths = new[]
{
"/oauth2redirect",
})]
public class GoogleUrlSchemeInterceptorActivity : UrlSchemeInterceptor { }

关于android - Xamarin.Auth (Android) - Chrome 自定义选项卡不会在重定向时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45350947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com