c# - 来自 Windows 应用商店应用程序 (WebAuthenticationBroker.AuthenticateAsync) 的 Facebook 身份验证 (OAuth2) 在桌面计算机上失败

标签 c# facebook authentication windows-store-apps

我有一个已发布的 Xamarin.Forms 应用。该应用程序建议用户通过多个 OAuth 身份验证提供商(Google、Facebook、Microsoft、Yandex、Vkontakte、Mail.Ru、Odnoklassniki)进行身份验证。

在 Windows 上,通过 Facebook 的身份验证是按以下方式构建的:

string clientID = "<client ID from facebook app settings>";

string startUri = "https://m.facebook.com/dialog/oauth/?" + 
    "client_id=" + clientID +
    "&scope=" + "email,public_profile" + 
    "&redirect_uri=" + "https://m.facebook.com/connect/login_success.html" + 
    "&state=" + Guid.NewGuid().ToString("N") + 
    "&response_type=" + "token";

WebAuthenticationBroker.AuthenticateAsync(
    WebAuthenticationOptions.None,
    new Uri(startUri),
    new Uri("https://m.facebook.com/connect/login_success.html"));

相同的原理图但具有不同的 Uris 用于其他身份验证提供程序并且工作正常。 Facebook 身份验证在 Windows Phone 上有效,但在 Windows 10 和 Windows 8.1 台式计算机上失败。失败场景:

  1. 出现 WebAuthenticationBroker 窗口。 The WebAuthenticationBroker windows appears

  2. Facebook 身份验证对话框内容会显示几秒钟。 The facebook authentication dialog content appears for a couple of seconds

  3. Facebook 身份验证对话框内容消失,取而代之的是以下文本:“我们现在无法连接到您需要的服务。请检查您的网络连接或稍后重试。 ”。 We can't connect to the service you need right now. Check your network connection or try this again later.

在第 2 步和第 3 步之间,我的应用程序代码均未执行。在许多不同的计算机上也会发生同样的情况。我尝试从 m.facebook.com 替换 Uri至 www.facebook.com没有效果。

Task<WebAuthenticationResult>WebAuthenticationBroker.AuthenticateAsync 返回以 IsFaulted 结束状态和内部的以下异常:

System.AggregateException: One or more errors occurred. ---> System.Exception: Exception from HRESULT: 0x800C0503

--- End of inner exception stack trace ---

---> (Inner Exception #0) System.Exception: Exception from HRESULT: 0x800C0503<---

我应该在哪里进一步挖掘?

最佳答案

在这个问题上,我一直在与 Facebook SDK 支持和 Microsoft 支持密切合作。这很像同时从两侧受到撞击,同时试图让自己愉快。感觉自己被宠坏了,浪费了,但仍然很满足。

长话短说,我有一个解决方法,现在只用于 Facebook/Windows 桌面组合:

//We need to instruct the WebAuthenticationBroker to end the scenario
//somehow without specifying the callbackUri parameter. This is called 
//`an implicit callback Uri scenario` in which WebAuthenticationBroker
//is expecting to see this Uri for the end:
string callbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri;

//So we need to instruct Facebook authentication to finally redirect to this Uri.
string startUri = "https://m.facebook.com/dialog/oauth/?" + 
    "client_id=" + clientID +
    "&scope=" + "email,public_profile" + 
    "&redirect_uri=" + callbackUri + 
    "&state=" + Guid.NewGuid().ToString("N") + 
    "&response_type=" + "token";

//The workaround is to go with the WebAuthenticationBroker.AuthenticateAsync
//overload which does not accept the callbackUri parameter.
WebAuthenticationBroker.AuthenticateAsync(
    WebAuthenticationOptions.None,
    new Uri(startUri)));

Facebook 和 Microsoft 都没有做任何事情来帮助解决这个问题,也没有提供任何合乎逻辑的解释来解释为什么这个场景必须用于 Windows 桌面。我再次提到这一点:在 Windows Phone 8.1/10 上,显式回调 Uri 方案完美运行。

我必须提到,为了让 Facebook 身份验证接受这样的隐式回调 URI,您需要将这些 URI 包含到 Facebook login有效 OAuth 重定向 URI 设置中。您的应用的参数。

  1. https://www.facebook.com/dialog/return/ms
  2. https://m.facebook.com/dialog/return/ms

...Facebook 的又一设计杰作。啊。

关于c# - 来自 Windows 应用商店应用程序 (WebAuthenticationBroker.AuthenticateAsync) 的 Facebook 身份验证 (OAuth2) 在桌面计算机上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38741950/

相关文章:

c# - 是否可以记录谁启动或停止了 Windows 服务?

javascript - 如果选中复选框,则在 Facebook 墙上发布消息

c# - 如何记住登录表单的用户名或密码

java - 使用 ACTION_SEND Intent 时,Facebook 在 Android 上剪辑文本

android - 检查用户的 Facebook 个人资料是否已更新

asp.net-mvc - Web API 和 AngularJS SPA 应用程序的身份验证

php - CRYPT_BLOWFISH 用法

具有多种类型的 C# 泛型类型推断

c# - Silverlight 文本修剪和换行问题

c# - 如何使用 C# 应用程序获取图像标签