【问题标题】:Auth0.SDK.AuthenticationCancelException in Windows Phone 8.0 projectWindows Phone 8.0 项目中的 Auth0.SDK.AuthenticationCancelException
【发布时间】:2014-10-24 22:15:30
【问题描述】:

我正在为 Windows Phone 8.0 项目使用 Auth0Client。我对 Auth0.SDK.AuthenticationCancelException 有疑问 - 因为在单击 LoginFacebookTap 按钮后按下返回按钮时,它一直在使我的应用程序崩溃。

所以当我加载应用程序时,我会转到 LoginView 页面,并且有一个按钮会调用:

private async void LoginFacebookTap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var result = await authorizationService.LoginAuth0(AuthorizationService.AuthorizationServiceType.Facebook, "", "");
    if (result == true)
    {
        MessageBox.Show("OK!");
    }
}

这是我的方法 LoginAuth0

public async Task<Boolean> LoginAuth0(AuthorizationServiceType type, string email, string password)
{
    if (NetworkService.IsNetworkAvailable())
    {
        try
        {
            Auth0User user;
            switch (type)
            {
                case AuthorizationServiceType.EmailAndPassword:
                    user = await auth0.LoginAsync(Constants.login_with_password_Auth0, email, password);
                    break;
                case AuthorizationServiceType.Email:
                    user = await auth0.LoginAsync(Constants.login_azure_Auth0);
                    break;
                case AuthorizationServiceType.Facebook:
                    user = await auth0.LoginAsync(Constants.login_facebook_Auth0);
                    break;
                case AuthorizationServiceType.Google:
                    user = await auth0.LoginAsync(Constants.login_google_Auth0);
                    break;
                case AuthorizationServiceType.Linkedin:
                    user = await auth0.LoginAsync(Constants.login_linkedin_Auth0);
                    break;
                case AuthorizationServiceType.Twitter:
                    user = await auth0.LoginAsync(Constants.login_twitter_Auth0);
                    break;
                case AuthorizationServiceType.Windows:
                    user = await auth0.LoginAsync(Constants.login_windows_Auth0);
                    break;
            }
        }
        catch (AuthenticationCancelException)
        {
            System.Diagnostics.Debug.WriteLine("User press cancel on Authentication");
            return false;
        }
        catch (AuthenticationErrorException ex)
        {
            MessageBox.Show(Resources.AppResources.TitleError + " : " + ex.Message);
            return false;
        }
        catch (Exception exc)
        {
            MessageBox.Show(Resources.AppResources.MessageErrorUserNotExists);
            System.Diagnostics.Debug.WriteLine("Unknown exception: "+exc.Message);
            return false;
        }

        try
        {
            var targetClientId = Constants.clientId_Auth0;
            var options = new Dictionary<string, string>
            {
                { "scope", "openid profile" }
            };
            var delegationResult = await auth0.GetDelegationToken(targetClientId, options);

            if (delegationResult != null)
            {
                String jsonString = delegationResult["id_token"].ToString();
                System.Diagnostics.Debug.WriteLine("Application received token: " + jsonString);
                return true;
            }
            else MessageBox.Show(AppResources.MessageErrorSigning);
        }
        catch(Exception exc)
        {
            MessageBox.Show(Resources.AppResources.TitleError +" : " + exc.Message);
        }
        return false;
    }
    else
    {
        MessageBox.Show(Resources.AppResources.MessageErrorNoConnection);
        return false;
    }
}

在我点击 LoginFacebookTap 并按 Back 按钮后,结果如下:

类型的第一次机会异常 'Auth0.SDK.AuthenticationCancelException' 发生在 Auth0Client.Phone.Silverlight.ni.DLL 类型的第一次机会异常 mscorlib.ni.dll 中发生“Auth0.SDK.AuthenticationCancelException” 类型的第一次机会异常 mscorlib.ni.dll 中发生“Auth0.SDK.AuthenticationCancelException” “System.InvalidOperationException”类型的第一次机会异常 发生在 Microsoft.Phone.ni.dll 中

然后应用程序就崩溃了,“当 CanGoBack 为假时,InvalidOperationException 无法返回。”。是bug还是?

更新

我发现只有当您登录您的 Facebook 帐户并通过身份验证时才会发生这种情况。然后,当您再次运行应用程序并单击 LoginFacebookTap 时,您不会被重定向到 facebook 登录页面,但您会自动重定向回来,并显示“OK”身份验证结果。在您被重定向回 LoginView 之前,您会看到白屏,当您按 Back 时,您将返回“InvalidOperationException CanGoBack 为 false 时无法返回。”

更新 2

我在 Auth0.SDK 中发现的:

/// <summary>
/// Handler for the browser control's navigation failed event.  We use this to detect errors
/// </summary>
private void BrowserControl_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
    WebBrowserNavigationException navEx = e.Exception as WebBrowserNavigationException;

    if (navEx != null)
    {
        // Pass along the provided error information.
        responseErrorDetail = string.Format("Error code: {0}", navEx.StatusCode);
    }
    else
    {
        // No error information available.
        responseErrorDetail = NoDetailsAvailableMessage;
    }
    responseStatus = PhoneAuthenticationStatus.ErrorHttp;

    authenticationFinished = true;
    e.Handled = true;

    // Navigate back now.
    this.NavigateBackWithProgress();
}

/// <summary>
/// Displays the progress bar and navigates to the previous page.
/// </summary>
private void NavigateBackWithProgress()
{
    ShowProgressBar();
    NavigationService.GoBack();
}

在 NavigationService.GoBack();我们有 InvalidOperationException CanGoBack 为 false 时无法返回。

【问题讨论】:

  • 所以你是说 try/catch 不起作用?
  • 没有。我是说我不知道​​这是从哪里来的。

标签: c# silverlight authentication windows-phone-8


【解决方案1】:

是的,当您取消身份验证时,SDK 会引发异常。你抓到了,没关系

【讨论】:

  • 并非如此。抛出 SDK 不是异常,而是异常,因为在调用 NavigationService.GoBack() 之前,我们应该始终检查后堆栈上是否有偶数页。我克隆 SDK 并添加 if(NavigationService.CanGoBack) 检查。
【解决方案2】:

这是 SDK 的错误:

private void NavigateBackWithProgress()
 {
     ShowProgressBar();
    if(NavigationService.CanGoBack)
        NavigationService.GoBack();
 }

在调用 NavigationService.GoBack() 之前,我们应该始终检查后堆栈上是否有偶数页。也指:

private void NavigateBackWithProgress()
 {
     ShowProgressBar();
    if(this.Frame != null && this.Frame.CanGoBack)
        this.Frame.GoBack();
 }

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多