首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当WebAuthenticationBroker接收到OAuth2回调时引发的异常

当WebAuthenticationBroker接收到OAuth2回调时引发的异常
EN

Stack Overflow用户
提问于 2013-02-07 03:23:03
回答 2查看 2.5K关注 0票数 4

WebAuthenticationBroker似乎无法处理到我的ms-app://的导航。就像下面看到的那样,抛出这个丑陋的错误。

步骤

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

WebAuthenticationBroker.AuthenticateAsync()的回调被接收(根据Fiddler4 & Event ),但是它抛出上述异常,就好像它不知道如何解释ms-app://协议一样。

所有的例子都暗示我的代码应该能工作,但是我认为有一些不太明显的问题引起了问题。

代码

代码语言:javascript
运行
复制
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身份验证问题(Windows)。不幸的是,这是查询authhost.exe导航错误时的唯一搜索结果。

  1. InformationAuthHost 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. InformationAuthHost encountered Meta Tag: mswebdialog-title with content: <Can't connect to the service>.

谢谢你的阅读,斯塔克。别让我失望了!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-08 13:43:50

@ma_il帮助我理解了代理是如何评估重定向回调的,它使我回到了原点,在那里我意识到WebAuthenticationOptions.UseTitle是正确的用法。不是。它需要使用WebAuthenticationOptions.None,并且可以立即工作。

作为未来寻找答案者的一个例子,这是我的代码。

代码语言:javascript
运行
复制
    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; }
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-07 10:44:52

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14742974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档