WebAuthenticationBroker

WebAuthenticationBroker

本文介绍了WebAuthenticationBroker 收到 OAuth2 回调时抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebAuthenticationBroker 似乎无法处理到我的 ms-app:// 的导航.只是抛出这个丑陋的错误,你将在下面看到.

The WebAuthenticationBroker doesn't seem to be able to handle navigation to my ms-app://. Just throws this ugly error as you will see below.

  1. 调用AuthenticateAsync(),包括运行时获取的回调uri:WebAuthenticationBroker.GetCurrentApplicationCallbackUri()
  2. 完成授权流程,点击允许.
  3. 代理没有返回,而是显示页面无法连接到服务.我们现在无法连接到您需要的服务. 无法执行任何操作,所以我点击了可见的返回"按钮.
  4. 调试器在捕获时中断:指定的协议未知.(来自 HRESULT 的异常:0x800C000D)"
  1. Call AuthenticateAsync(), including callback uri obtained at runtime: WebAuthenticationBroker.GetCurrentApplicationCallbackUri()
  2. Go through authorize process, hit Allow.
  3. Instead of returning, the broker shows the page Can't connect to service. We can't connect to the service you need right now. Unable to do anything, so I hit the Back button visible.
  4. Debugger breaks on catch: "The specified protocol is unknown. (Exception from HRESULT: 0x800C000D)"

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

The callback for WebAuthenticationBroker.AuthenticateAsync() is received (according to Fiddler4 & the Event Viewer) but it throws the aforementioned exception as if it doesn't know how to interpret the ms-app:// protocol.

所有示例都暗示我的代码应该可以工作,但我认为有一些不太明显的原因导致了问题.

All examples imply my code should work but I think there's something less obvious causing an issue.

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 导航错误时唯一的搜索结果.

Event Viewer log excerpts (chronological order)

For information on how I obtained this, read the following MSDN: Web authentication problems (Windows). Unfortunately this is the only search result when querying authhost.exe navigation error.

  1. 信息:AuthHost 重定向到 URL:<ms-app://s-1-15-2-504558873-2277781482-774653033-676865894-877042302-143455173737#access_token=------&expires_in=3600&token_type=bearer&refresh_token=------&account_username=------>来自 URL:<https://api.imgur.com/oauth2/authorize?client_id=------&response_type=token>使用 HttpStatusCode: 302.
  2. 错误:AuthHost 在 URL 处遇到导航错误:<https://api.imgur.com/oauth2/authorize?client_id=------&response_type=令牌>状态代码:0x800C000D.
  3. 信息:AuthHost 遇到元标记:mswebdialog-title 内容:.

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

Thanks for reading, Stack. Don't fail me now!

推荐答案

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

Resolved! @ma_il helped me understand how the broker actually evaluates the redirect callback and it led me back to square one where I realized I assumed WebAuthenticationOptions.UseTitle was the proper usage. Not so. Up against Imgur's API using a token, it requires WebAuthenticationOptions.None and it worked immediately.

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

As an example to future answer-seekers, here's my code.

    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; }
}

这篇关于WebAuthenticationBroker 收到 OAuth2 回调时抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:31