我正在将Azure移动服务身份验证添加到通用Windows项目中。它在服务器和应用程序的Windows应用商店版本中都已设置并正常工作,但我无法使它与应用程序的WindowsPhone8.1版本一起工作。事实上,我有两个不同的应用程序一直在处理同一个问题,所以我严格按照本文中概述的步骤创建了一个测试应用程序。示例应用程序在UI上有一个按钮,按下该按钮时将尝试使用Twitter对用户进行身份验证。
我从UI中看到的流程是:
- 按下按钮
- 屏幕暂时变黑
- 屏幕显示带有微调器的"继续…"
- InvalidOperationException被捕获
异常详细信息:
捕捉到System.InvalidOperationExceptionHResult=-2146233079消息=用户取消了身份验证。来源=mscorlib
StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.WindowsAzure.MobileServices.MobileServiceAuthentication.<LoginAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at MobileAuthTest.MainPage.<AuthenticateAsync>d__3.MoveNext() InnerException:
我使用的代码几乎是逐字逐句地从文章中复制的,但我也会把它粘贴在这里:
private async void Button_Click(object sender, RoutedEventArgs e)
{
await this.AuthenticateAsync();
}
// Define a method that performs the authentication process
// using a Twitter sign-in.
private async Task AuthenticateAsync()
{
while (user == null)
{
string message;
try
{
// Change 'MobileService' to the name of your MobileServiceClient instance.
// Sign-in using Twitter authentication.
user = await App.mobileService
.LoginAsync(MobileServiceAuthenticationProvider.Twitter);
message =
string.Format("You are now signed in - {0}", user.UserId);
}
catch (InvalidOperationException)
{
message = "You must log in. Login Required";
}
var dialog = new MessageDialog(message);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
}
这个覆盖在App.xaml.cs:中
protected override void OnActivated(IActivatedEventArgs args)
{
// Windows Phone 8.1 requires you to handle the respose from the WebAuthenticationBroker.
#if WINDOWS_PHONE_APP
if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation)
{
// Completes the sign-in process started by LoginAsync.
// Change 'MobileService' to the name of your MobileServiceClient instance.
mobileService.LoginComplete(args as WebAuthenticationBrokerContinuationEventArgs);
}
#endif
base.OnActivated(args);
}
我的测试都是在一台真正的Windows Phone 8.1设备上进行的(诺基亚920 8.1更新版)。
同样值得指出的是,我目前正在尝试的项目也在使用Xamarin.Forms for Android、iOS和Windows Phone(我想比较一下Xamarin.Forms for Windows Phone和Universal应用程序for Windows Phone)。除两个Windows Phone应用程序外,所有应用程序都可以工作。Xamarin.Forms Windows Phone应用程序是一个Windows Phone Silverlight 8.1应用程序,其操作与上面针对通用WP所描述的相同,但失败的原因是错误"身份验证失败,HTTP响应代码为0。"
我到处找了找,没有发现其他人遇到和我一样的问题。我需要做的事情这么简单吗?没有人觉得有必要用书面形式说出来?
事实证明,我看到的问题可能只是我的设备。我将示例应用程序发送给另一位开发人员进行测试,他没有任何问题。我还能够在模拟器上成功地使用示例应用程序和真实应用程序登录。这并不完全是一个彻底的测试样本,但它足以让我停止把头撞在这张桌子上。