我正在使用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后按“返回”按钮,结果为:
类型的第一次机会异常 '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时无法返回。”这是bug还是?
我发现,只有当您登录到自己的Facebook帐户并且通过身份验证时才会发生这种情况。然后,当您再次运行应用程序并单击LoginFacebookTap时,您将不会重定向到Facebook登录页面,但您将自动重定向并返回“OK”验证结果。在重定向回LoginView之前,您会看到白屏,当您按Back时,将返回“InvalidOperationException当CanGoBack为false时无法返回。”
我在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时无法返回。
答案 0 :(得分:0)
是的,当您取消身份验证时,SDK会引发异常。你正在捕捉它,所以没关系
答案 1 :(得分:0)
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();
}