Windows Phone 8.0项目中的Auth0.SDK.AuthenticationCancelException
本文关键字:Auth0 SDK AuthenticationCancelException 项目 Phone Windows | 更新日期: 2023-09-27 18:27:37
我正在使用Auth0Client for Windows Phone 8.0项目。我对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 中
然后应用程序崩溃,并显示"InvalidOperationException CanGoBack为false时无法返回。"。这是错误还是?
更新
我发现,只有当你登录到你的脸书账户并通过身份验证时,才会发生这种情况。然后,当你再次运行应用程序并点击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();当CanGoBack为false时,我们有InvalidOperationException无法返回。
是的,当您取消身份验证时,SDK会抛出异常。你正在捕捉它,所以这没关系
这是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();
}