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

C# 来自Windows应用商店应用程序(WebAuthenticationBroker.AuthenticationAsync)的Facebook身份验证(OAuth2)在桌面计算机上失败,c#,facebook,authentication,windows-store-apps,C#,Facebook,Authentication,Windows Store Apps,我有一个已发布的Xamarin.Forms应用程序。该应用建议用户通过几个OAuth认证提供商(谷歌、Facebook、微软、Yandex、Vkontakte、Mail.Ru、Odnoklassniki)进行认证 在Windows上,通过Facebook进行身份验证的方式如下: string clientID = "<client ID from facebook app settings>"; string startUri = "https://m.facebook.com/d

我有一个已发布的Xamarin.Forms应用程序。该应用建议用户通过几个OAuth认证提供商(谷歌、Facebook、微软、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"));
string clientID=“”;
字符串startUri=”https://m.facebook.com/dialog/oauth/?" + 
“client_id=“+clientID+
“&scope=“+”电子邮件、公共_配置文件”+
“&重定向_uri=“+”https://m.facebook.com/connect/login_success.html" + 
“&state=“+Guid.NewGuid().ToString(“N”)+
“&response_type=“+”令牌”;
WebAuthenticationBroker.AuthenticationAsync(
WebAuthenticationOptions。无,
新Uri(startUri),
新Uri(“https://m.facebook.com/connect/login_success.html"));
对于其他身份验证提供程序,使用相同的原理图但具有不同的URI,效果良好。Facebook身份验证在windows Phone上运行,但在windows 10和windows 8.1桌面计算机上失败。故障场景:

  • 此时将显示WebAuthenticationBroker窗口。

  • facebook身份验证对话框内容会出现几秒钟。

  • facebook身份验证对话框内容消失,出现以下文本:“我们现在无法连接到您需要的服务。请检查您的网络连接或稍后再试。”。

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

    WebAuthenticationBroker.authenticateSync
    返回的
    任务
    IsFaulted
    状态结束,内部出现以下异常:

    System.AggregateException:发生一个或多个错误。--> 系统异常:来自HRESULT的异常:0x800C0503

    ---内部异常堆栈跟踪的结束---


    --->(内部异常#0)系统。异常:来自HRESULT的异常:0x800C0503将此参数添加到startUri“display=popup”,因此您的代码应该如下所示:

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

    display参数的其他可能值有:async、iframe、page、popup、touch、wap。

    在这个问题上,我一直与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和微软都没有做任何有助于解决这一问题的事情,也没有提供任何逻辑上的解释,说明为什么这个场景必须用于Windows桌面。我再次提到:在WindowsPhone8.1/10上,显式回调Uri场景非常有效

    我必须提到,为了让Facebook身份验证接受这样一个隐式回调Uri,您需要将这些Uri包括到应用程序的
    Facebook登录
    参数的有效OAuth重定向Uri设置中


  • …Facebook的另一个设计杰作。啊。

    我尝试了
    display=popup
    选项。什么都不会改变。