【问题标题】:Exception thrown when WebAuthenticationBroker receives an OAuth2 callbackWebAuthenticationBroker 收到 OAuth2 回调时抛出异常
【发布时间】:2013-01-22 11:19:35
【问题描述】:

WebAuthenticationBroker 似乎无法处理到我的ms-app:// 的导航。只是抛出这个丑陋的错误,如下所示。

步骤

  1. 调用AuthenticateAsync(),包括运行时获取的回调uri:WebAuthenticationBroker.GetCurrentApplicationCallbackUri()
  2. 完成授权过程,点击允许
  3. 代理没有返回,而是显示页面无法连接到服务。我们现在无法连接到您需要的服务。 无法执行任何操作,所以我点击了可见的“后退”按钮。
  4. 调试器在捕获时中断:"The specified protocol is unknown. (Exception from HRESULT: 0x800C000D)"

接收到WebAuthenticationBroker.AuthenticateAsync() 的回调(根据 Fiddler4 和事件查看器),但它抛出上述异常,好像它不知道如何解释 ms-app:// 协议。

所有示例都表明我的代码应该可以工作,但我认为有一些不太明显的东西会导致问题。

代码

private static string authorizeString =
  "https://api.imgur.com/oauth2/authorize?client_id=---------&response_type=token";

private Uri startUri = new Uri(authorizeString);

public async void RequestToken() {
  try {
    var war = await WebAuthenticationBroker.AuthenticateAsync(
      WebAuthenticationOptions.UseTitle
      , startUri);
      // Imgur knows my redirect URI, so I am not passing it through here

    if (war.ResponseStatus == WebAuthenticationStatus.Success) {
      var token = war.ResponseData;
    } 
  } catch (Exception e) { throw e; }
}

事件查看器日志摘录(按时间顺序)

有关我如何获得此信息的信息,请阅读以下 MSDN:Web authentication problems (Windows)。不幸的是,这是查询 authhost.exe 导航错误时唯一的搜索结果。

  1. 信息AuthHost redirected to URL: <ms-app://s-1-15-2-504558873-2277781482-774653033-676865894-877042302-1411577334-1137525427/#access_token=------&expires_in=3600&token_type=bearer&refresh_token=------&account_username=------> from URL: <https://api.imgur.com/oauth2/authorize?client_id=------&response_type=token> with HttpStatusCode: 302.
  2. 错误AuthHost encountered a navigation error at URL: <https://api.imgur.com/oauth2/authorize?client_id=------&response_type=token> with StatusCode: 0x800C000D.
  3. 信息AuthHost encountered Meta Tag: mswebdialog-title with content: <Can't connect to the service>.

感谢您的阅读,堆栈。现在不要让我失望!

【问题讨论】:

    标签: windows-8 windows-runtime oauth-2.0 imgur


    【解决方案1】:

    Afaik,即使您假设远程服务知道它,您也需要将结束 URL 传递给 AuthenticateAsync。

    WebAuthenticationBroker 的工作方式如下:您指定一个“端点”URL,当它遇到以该 URL 开头的链接时,它会认为身份验证过程已完成,甚至不再尝试导航到该 URL。 因此,如果您将“foo://bar”指定为回调 URI,导航到“foo://bar”将完成身份验证,“foo://barbaz”也会完成身份验证,但不会“foo://baz”。

    【讨论】:

    • 我也试过这个,效果一样。我只是传入 CurrentApplicationCallbackUri() 并抛出该异常,说它不理解协议。此外,我终于获得了在我的 Surface 上注册的开发人员许可证,结果相同。所以我想这排除了对 Authhost.exe 被“破坏”的任何怀疑。
    • 如上所述,我成功地使用了带有非 http 回调 URI 的 WebAuthenticationBroker。能否用浏览器检查生成的 HTML 在服务器端是否正确?
    • 在我继续之前,您能澄清一下您的非 HTTP 回调 URI 吗?您是否使用了 ms-app:// 协议或其他协议?你必须注册它才能让它工作吗?
    • 我们使用不同的协议,无需注册即可使用。正如我试图解释的那样,WebAuthenticationBroker 只是对每个 URL 的开头进行字符串比较,并在 URL 的开头与给定的端点匹配时完成该过程。没有实际导航到此 URL。
    • 啊,这很有趣。您将如何验证重定向尝试导航到的实际 URL?与 Fiddler 有困难,所以这不是一个富有成效的努力。当我将构建的 URI 扔进浏览器并点击允许时,Imgur 的服务器根本没有响应。非常感谢您的帮助。
    【解决方案2】:

    解决了! @ma_il 帮助我了解了代理如何实际评估重定向回调,它让我回到了第一方,我意识到我认为 WebAuthenticationOptions.UseTitle 是正确的用法。 不是这样。与使用令牌的 Imgur 的 API 相比,它需要 WebAuthenticationOptions.None 并且可以立即工作。

    作为未来寻求答案的人的示例,这是我的代码。

        private const string clientId = "---------";
    private static Uri endUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
    private static string authorizeString = "https://api.imgur.com/oauth2/authorize?" 
                                              + "client_id=" 
                                              + clientId 
                                              + "&response_type=token" 
                                              + "&state=somestateyouwant" 
                                              + "&redirect_uri=" 
                                              + endUri;
    private Uri startUri = new Uri(authorizeString);   
    
    
    public async void RequestToken() {
      try {
        WebAuthenticationResult webAuthenticationResult =
          await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None
                                                          , startUri
                                                          , endUri);
    
        if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) {
          string token = webAuthenticationResult.ResponseData;
          // now you have the token
        }
      } catch { throw; }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 2018-10-18
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多