c# - 从 Metro 应用程序访问 Azure 中受 ACS 保护的 Web API 站点

标签 c# azure windows-8 acs

我有一个运行 ASP.Net Web API 应用程序的 Azure 辅助角色。这是受 ACS 保护的。当我使用浏览器浏览 Web api 时,ACS 要求我针对 Google 或 LiveId 进行身份验证。当我进行身份验证时,我可以看到数据。

我正在尝试从 Win 8 Metro 风格应用程序访问相同的 API。我正在尝试使用 WebAuthenticationBroker。

WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                                                        WebAuthenticationOptions.None,
                                                        new Uri("https://xxxxx.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2fyyyyy.cloudapp.net%2f"),
                                                        new Uri("http://yyyyy.cloudapp.net/")
                                                       );

当我启动应用程序时,它要求我像以前一样通过 Google 或 LiveId 进行身份验证。这通过了,我得到了成功的结果。

然后我使用 HttpClient 创建对 API 的调用:

    HttpClient client = new HttpClient();
    Uri _baseUri = new Uri("http://yyyyy.cloudapp.net/api/");
    client.BaseAddress = _baseUri;

    var response = client.GetAsync("Values/").Result;
    var responseBodyAsText = response.Content.ReadAsStringAsync().Result;

    var ids = JsonConvert.DeserializeObject<IList<int>>(responseBodyAsText);

这种方法有效,它似乎正确导航到 URI,但返回的有效负载是要求我登录的 HTML,而不是我期望的 JSON 数据。

我在这方面花费的时间超出了合理范围,而且已经没有想法了!有人可以帮忙吗?

最佳答案

在本地主机上测试时,您必须确保 ACS 可以访问您的计算机。

正如 @caleb 所写,您需要监听 token 并将其重定向到代理。 您可以查看我的帖子,其中包含一个工作示例以及如何设置它。

Does the WebAuthenticationBroker work in Windows 8 Metro App post Release Candidate

关于c# - 从 Metro 应用程序访问 Azure 中受 ACS 保护的 Web API 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11757978/

相关文章:

azure - 如何在terraform中使用azurerm_virtual_machine资源的custom_data字段?

windows-8 - 如何检测 XAML slider 何时完成?

c# - 比较 2 个字符串数组元素并返回

c# - 使用 WebClient 对网站执行 ping 操作

c# - 处理用户控件,真的意味着编辑 .designer.cs 文件吗?

Windows 8 关机过程与 Vista 不同?

c# - json 到 Windows 应用商店应用程序 C# 中的新列表框项目

c# - 如何在每次 IIS 重新启动时执行一些代码

azure - 使用 Graph beta 版本为企业应用程序创建新的角色分配

xml - Orckestra C1 - 决定使用 SQL 的转折点是什么?