1

我刚刚开始使用 Xamarin.Auth,我想通过 oauth 启用 Facebook 登录。

这是我的配置:

public static string ClientId = "client id";
public static string ClientSecret = "client secret";
public static string Scope = "email";
public static string AuthorizeUrl = "https://m.facebook.com/dialog/oauth";
public static string RedirectUrl = "https://www.facebook.com/connect/login_success.html";
public static string AccessTokenUrl = "https://m.facebook.com/dialog/oauth/token";

启动认证的代码:

public class AuthenticationPageRenderer : PageRenderer
{
    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear (animated);

        var auth = new OAuth2Authenticator (
            Constants.ClientId,
            Constants.ClientSecret,
            Constants.Scope,
            new Uri (Constants.AuthorizeUrl),
            new Uri (Constants.RedirectUrl),
            new Uri (Constants.AccessTokenUrl)
        );

        auth.Completed += OnAuthenticationCompleted;
        PresentViewController (auth.GetUI (), true, null);
    }

    async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e)
    {
        Debug.WriteLine ("AUTH Completed!");
        if (e.IsAuthenticated) {

        }
    }
}

似乎工作正常,但我不想将用户重定向到https://www.facebook.com/connect/login_success.html,而是想再次将他重定向回我的应用程序。非常感谢任何帮助!

最好的,萨沙

4

2 回答 2

2

您可以通过简单地调用自己的方法来再次“重定向回”到您的应用程序,以像这样显示您希望向用户显示的应用程序页面。

async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e)
{
    Debug.WriteLine ("AUTH Completed!");
    if (e.IsAuthenticated) {
        //invoke the method that display the app's page
        //that you want to present to user
        App.SuccessfulLoginAction.Invoke();
    }
}

在你的 App.cs

public static Action SuccessfulLoginAction
    {
        get
        {    
            return new Action(() =>
            {
                //show your app page
                var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
                masterDetailPage.Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(MainPage)));
                masterDetailPage.IsPresented = false;                    
            });
        }
    }

假设 MainPage 是您成功登录后要显示的页面。我正在使用 Xamarin.Forms 和 MasterDetailPage 在我的示例中显示可能与您的应用程序不同但概念相同的页面。

于 2016-05-24T18:09:13.333 回答
1

只需调用DismissViewController (true, null)您的OnAuthenticationCompleted方法即可。或使用异步等效项:

async void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
    Debug.WriteLine("AUTH Completed!");
    await DismissViewControllerAsync(true);
    if (e.IsAuthenticated)
    {

    }
}
于 2016-04-30T13:41:01.630 回答