【发布时间】:2015-06-12 07:51:06
【问题描述】:
我正在向通用 Windows 项目添加 Azure 移动服务身份验证。它在服务器和应用程序的 Windows 应用商店版本中都设置并正常工作,但是我无法让它与应用程序的 Windows Phone 8.1 版本一起工作。实际上,我有两个不同的应用程序一直在处理相同的问题,因此我严格按照this article 中概述的步骤创建了一个测试应用程序。示例应用在 UI 上有一个按钮,当按下该按钮时,它将尝试使用 Twitter 对用户进行身份验证。
我从 UI 中看到的流程是:
- 按下按钮
- 屏幕暂时变黑
- 屏幕显示带有微调器的“正在恢复...”
- InvalidOperationException 被捕获
异常详情:
System.InvalidOperationException 被捕获 H结果=-2146233079 Message=身份验证已被用户取消。 来源=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 更新)。
还值得指出的是,我目前正在尝试使其工作的项目也使用适用于 Android、iOS 和 Windows Phone 的 Xamarin.Forms(我想比较适用于 Windows Phone 和通用应用程序的 Xamarin.Forms对于 Windows 手机)。除了两个 Windows Phone 应用程序之外,所有应用程序都可以工作。 Xamarin.Forms Windows Phone 应用是 Windows Phone Silverlight 8.1 应用,其作用与上述通用 WP 相同,但失败并显示错误“身份验证失败,HTTP 响应代码 0”。
我四处搜寻,没有发现任何其他人遇到与我相同的问题。有没有我需要做的事情,这么简单,没人觉得有必要书面说出来?
【问题讨论】:
标签: windows-phone-8.1 azure-mobile-services win-universal-app