C#学习教程:WebAuthenticationBroker是否在Windows8MetroApp的Release8候选中工作1)现在确保在本地主机上测试您已经为本地主机端口上的入站设置了Windows防火墙。路由器上的端口转发(如果有)。2)然后你需要告诉IISExpress它可以从本地主机请求站点:找到文档IISExpress配置并编辑applicationhost.config。在列表中找到您的站点并从绑定中删除localhost。2a)ISS需要以管理员身份运行,以管理员身份运行visualstudio也以管理员身份启动iss...3)找到您的IP,www.myip.com并将ACS返回uri更改为:http:http://90.90。90.90:909090/api/federation/:http://90.90.90.90:909090/api/federation/4)更改webbroker以使用您的ip:WebAuthenticationResultwebAuthenticationResult=awaitWebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None(newUri"https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a48451%2f"),newUri("http://99.99.99.99:909090/api/联邦/结束"));一切都像这样对我有用。我有一个helloworld作为令牌传递到我的地铁应用程序。问题我已经设置了WCF服务和Metro应用程序。WCF服务设置为使用AzureACS进行身份验证。我编写了一个适用于WebService和ACS的控制台应用程序:staticvoidMain(string[]args){try{//首先启动web项目,然后是客户端WebClientclient=newWebClient();vartoken=检索ACSToken();client.Headers.Add("授权",token);client.Headers.Add("内容类型","text/xml");varurl=newUri("http://traffictheory.azurewebsites.net/UserService.svc/Users");//varurl=newUri("http://localhost:4000/UserService.svc/Users");//流stream=client.OpenRead(url);StreamReaderreader=newStreamReader(stream);字符串响应=reader.ReadToEnd();控制台。写(响应);}catch(Exceptionex){Console.Write(ex.Message);}控制台.ReadLine();}privatestaticstringRetrieveACSToken(){varacsHostName=ConfigurationManager.AppSettings.Get("ACSHostName");varacsNamespace=ConfigurationManager.AppSettings.Get("ACSNamespace");varusername=ConfigurationManager.AppSettings.Get("ServiceIdentityUserName");var密码=ConfigurationManager.AppSettings.Get("ServiceIdentityCredentialPassword");varscope="http://traffictheory.azurewebsites.net/";//varscope="http://localhost:4000/";////从ACSWebClient请求令牌client=newWebClient();client.BaseAddress=string.Format("https://{0}.{1}",acsNamespace,acsHostName);NameValueCollection值=newNameValueCollection();values.Add("wrap_name",username);values.Add("wrap_password",密码);values.Add("wrap_scope",scope);byte[]responseBytes=client.UploadValues("WRAPv0.9","POST",值);字符串响应=Encoding.UTF8.GetString(responseBytes);stringtoken=response.Split('&').Single(value=>value.StartsWith("wrap_access_token=",StringComparison.OrdinalIgnoreCase)).Split('=')[1];vardecodedToken=string.Format("WRAPaccess_token="{0}"",HttpUtility.UrlDecode(token));返回解码令牌;当我想从我的Metro应用程序使用它时,我现在面临两个问题,第一个与服务无关,它与WebAuthenticationBroker有关。1)当我使用WebAuthenticationResultwebAuthenticationResult=awaitWebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,newUri("https://s-innovations.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2ftraffictheory.azurewebsites.net%2f"),newUri("https://s-innovations.accesscontrol.windows.net"));我可以使用LiveID、Facebook等登录。不能使用Google,因为ACS没有正确包含ID。但我没有得到任何令牌或声明。我只得到:https://s-innovations.accesscontrol.windows.net/v2/wsfederation?wa=wsignin1.0https://s-innovations.accesscontrol.windows。net/v2/facebook?cx=cHI9d3NmZWRlcmF0aW9uJn…cmFmZmljdGhlb3J5LmF6dXJld2Vic2l0ZXMubmV0JTJmJmlwPUZhY2Vib29rLTM1NTk5MjQ2NzgxNzc5OQ2&code=AQDagvqoXQ.......HowdidIgetthismovie'sfinalstatement:http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-858THisapp作品!2)上面显示的控制台应用程序已通过身份验证并在调用API时获取要传递给服务的令牌,我如何从地铁应用程序获取此令牌。更新我按照建议创建了控制器:[HttpPost]publicActionResultEnd(){returnJson("HelloWorld");我做了一个突破,看看它是否得到它。还没有。WebAuthenticationResultwebAuthenticationResult=awaitWebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,newUri("https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%1%3a482f"),newUri("http://localhost:909090/Federation/End"));在我的依赖方应用程序上,我有一个领域http://localhost:909090/返回Url:无(已尝试http://localhost:909090/Federation/End)响应数据现在包含:http://localhost:909090/联邦/结束。更新2我也尝试使用api控制器,正如您在另一篇文章中展示的那样:response.Headers.Add("Location","/api/federation/end?acsToken="+ExtractBootstrapToken());返回响应;}publicstringGet(){返回“你好世界”;}受保护的虚拟字符串ExtractBootstrapToken(){return"HelloWorld";现在,登录屏幕只是挂起并以您正在寻找的服务结束,尚未准备好(或类似的东西)。acs返回urlhttp://localhost:48451/api/FederationWebAuthenticationResultwebAuthenticationResult=awaitWebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,newUri("https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a909090%2f"),newUri("http://localhost:909090/api/federation/end"));WebAuthenticationBroker只是一直浏览,直到下一个请求的页面是callbackUri参数指定的页面。此时它会返回最终的URL给你,所以如果你想获取任何东西,你需要在那个URL中对其进行编码。在依赖方的ACS控制面板中,您需要在站点的某处指定返回URL。例如https://traffictheory.azurewebsites.net/federationcallback。然后创建一个控制器来处理接受到该URL的帖子。该帖子将有一个表单字段wresult,它是一些xml,其中将包含从ACS返回的令牌。然后,您可以通过重定向到https://traffictheory.azurewebsites.net/federationcallback/end?token={whateveryouwanttoreturn}将令牌发送回WebAuthenticationBroker。然后,您需要将您的身份验证代理使用更改为以下内容:varwebAuthenticationResult=awaitWebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,newUri("https://s-innovations.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2ftraffictheory.azurewebsites.net%2f"),newUri("https://traffictheory.azurewebsites.net/federationcallback/end"));//你返回的数据vartoken=authenticateResult.ResponseData.Substring(authenticateResult.ResponseData.IndexOf("token=",StringComparison.Ordinal)+6);我处理身份验证回调帖子的控制器如下所示。公共类FederationcallbackController:ApiController{publicHttpResponseMessagePost(){varresponse=this.Request.CreateResponse(HttpStatusCode.Redirect);response.Headers.Add("Location","/api/federationcallback/end?acsToken="+ExtractBootstrapToken());返回响应;}受保护的虚拟字符串ExtractBootstrapToken(){returnHttpContext.Current.User.BootstrapToken();BootstrapToken()扩展方法是wif.swtNuGet包的一部分。默认情况下,通过在web.config的
