gpt4 book ai didi

c# - WebAuthenticationBroker 是否在 Windows 8 Metro App 发布候选版本中工作

转载 作者:行者123 更新时间:2023-11-30 22:25:03 26 4
gpt4 key购买 nike

解决方案我的工作解决方案可以在答案或更新二中找到。

1) 现在确保,为了在本地主机上进行测试,您已经为本地主机端口上的入站设置了 Windows 防火墙。路由器上的端口转发(如果有的话)。

2) 然后你需要告诉 IIS Express 请求来自本地主机之外是可以的:找到 Documents\IISExpress\config 并编辑 applicationhost.config。在列表中找到您的站点并从绑定(bind)中删除本地主机

        <site name="S-Innovations.TrafficTheory.Web2" id="1591449597">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="G:\Documents\Visual Studio 2012\Projects\S-Innovations.TrafficTheory\S-Innovations.TrafficTheory.Web2" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:909090:localhost" />
</bindings>
</site>

2a) ISS需要以管理员身份运行,以管理员身份运行visual studio也以管理员身份启动iss...

3) 找到您的 ip,www.myip.com 并将 ACS 返回 uri 更改为:http://90.90.90.90:909090/api/federation/

4) 更改 webbroker 以使用您的 ip:

        WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri("https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a48451%2f"),
new Uri("http://99.99.99.99:909090/api/federation/end"));

一切都像这样对我有用。我将一个 hello world 作为 token 传递到我的 Metro 应用程序。

问题

我已经设置了一个 WCF 服务和一个 Metro 应用程序。WCF 服务设置为使用 Azure ACS 进行身份验证。

我制作了一个与 WebService 和 ACS 一起工作的控制台应用程序:

            static void Main(string[] args)
{
try
{

// First start the web project, then the client
WebClient client = new WebClient();
var token = RetrieveACSToken();
client.Headers.Add("Authorization", token);
client.Headers.Add("Content-type", "text/xml");
var url = new Uri("http://traffictheory.azurewebsites.net/UserService.svc/Users");
//var url = new Uri("http://localhost:4000/UserService.svc/Users");//
Stream stream = client.OpenRead(url);
StreamReader reader = new StreamReader(stream);
String response = reader.ReadToEnd();
Console.Write(response);

}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.ReadLine();
}

private static string RetrieveACSToken()
{
var acsHostName = ConfigurationManager.AppSettings.Get("ACSHostName");
var acsNamespace = ConfigurationManager.AppSettings.Get("ACSNamespace");
var username = ConfigurationManager.AppSettings.Get("ServiceIdentityUserName");
var password = ConfigurationManager.AppSettings.Get("ServiceIdentityCredentialPassword");
var scope = "http://traffictheory.azurewebsites.net/";
//var scope = "http://localhost:4000/";//
// request a token from ACS
WebClient client = new WebClient();
client.BaseAddress = string.Format("https://{0}.{1}", acsNamespace, acsHostName);
NameValueCollection values = new
NameValueCollection();
values.Add("wrap_name", username);
values.Add("wrap_password", password);
values.Add("wrap_scope", scope);
byte[] responseBytes =
client.UploadValues("WRAPv0.9", "POST", values);
string response =
Encoding.UTF8.GetString(responseBytes);
string token = response
.Split('&')
.Single(value =>
value.StartsWith("wrap_access_token=",
StringComparison.OrdinalIgnoreCase))
.Split('=')[1];
var decodedToken = string.Format("WRAP access_token=\"{0}\"", HttpUtility.UrlDecode(token));
return decodedToken;
}

当我想从我的 Metro 应用程序中使用它时,我现在面临两个问题。第一个与服务无关,是关于 WebAuthenticationBroker 的。

1) 当我使用

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

我可以使用 LiveID、Facebook 等登录。不是谷歌,因为 ACS 没有正确包含 ID。但我没有得到任何形式的代币或 claim 。我只得到:

https://s-innovations.accesscontrol.windows.net/v2/wsfederation?wa=wsignin1.0https://s-innovations.accesscontrol.windows.net/v2/facebook?cx=cHI9d3NmZWRlcmF0aW9uJn...cmFmZmljdGhlb3J5LmF6dXJld2Vic2l0ZXMubmV0JTJmJmlwPUZhY2Vib29rLTM1NTk5MjQ2NzgxNzc5OQ2...qo=AXQDag

我如何获得像这部电影结尾那样的声明: http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-858T他的应用程序有效!

2)上面显示的控制台应用程序在调用 API 时获得身份验证并获取 token 以传递给服务,我如何从 Metro 应用程序中获取此 token 。

更新

我按照建议创建了 Controller :

[HttpPost]
public ActionResult End()
{
return Json("Hello World");
}

我已经设置了一个断点,看看它是否能成功。还没有命中。

    WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri("https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a48451%2f"),
new Uri("http://localhost:909090/Federation/End"));

我有依赖方申请

Realm http://localhost:909090/
Return Url: Nothing (have tried http://localhost:909090/Federation/End )

响应数据包含:http://localhost:909090/Federation/End 现在。

更新 2

我也试过你在另一篇文章中展示的 api Controller :

公共(public)类 FederationController : ApiController{

public HttpResponseMessage Post()
{
var response = this.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Add("Location", "/api/federation/end?acsToken=" + ExtractBootstrapToken());

return response;
}
public string Get()
{
return "hello world";
}
protected virtual string ExtractBootstrapToken()
{
return "Hello World";
}

现在登录屏幕只是挂起并以您正在寻找的服务现在还没有准备好(或类似的东西)结束。

acs 返回 url http://localhost:48451/api/Federation

        WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri("https://traffictheory.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost%3a909090%2f"),
new Uri("http://localhost:909090/api/federation/end"));

最佳答案

WebAuthenticationBroker简单地继续浏览,直到下一个请求的页面是由 callbackUri 指定的页面范围。届时它会将最终 URL 返回给您,因此如果您想要取回任何内容,则需要在该 URL 中对其进行编码。

在依赖方的 ACS 控制面板中,您需要指定站点某处的返回 URL。例如https://traffictheory.azurewebsites.net/federationcallback .然后创建一个 Controller 来处理接受该 URL 的帖子。该帖子将有一个表单字段 wresult这是一些包含从 ACS 返回的 token 的 xml。

然后您可以将 token 发送回 WebAuthenticationBroker通过重定向到 https://traffictheory.azurewebsites.net/federationcallback/end?token={whatever you want to return}

然后您需要将身份验证代理的用法更改为以下内容:

var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri("https://s-innovations.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2ftraffictheory.azurewebsites.net%2f"),
new Uri("https://traffictheory.azurewebsites.net/federationcallback/end")
);

// The data you returned
var token = authenticateResult.ResponseData.Substring(authenticateResult.ResponseData.IndexOf("token=", StringComparison.Ordinal) + 6);

我用于处理身份验证回调帖子的 Controller 如下所示。

public class FederationcallbackController : ApiController
{
public HttpResponseMessage Post()
{
var response = this.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Add("Location", "/api/federationcallback/end?acsToken=" + ExtractBootstrapToken());

return response;
}

protected virtual string ExtractBootstrapToken()
{
return HttpContext.Current.User.BootstrapToken();
}
}

BootstrapToken()扩展方法是 wif.swt 的一部分NuGet 包。默认情况下,WIF 不会将任何内容保存到引导 token 属性中,您需要通过包含 saveBootstrapTokens="true" 来启用它<service> 上的属性<microsoft.identityModel> 下的元素在你的 web.config 中。我的看起来像这样:

<microsoft.identityModel>
<service saveBootstrapTokens="true">
<audienceUris>
<add value="http://localhost:3949/" />
</audienceUris>
<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" issuer="https://xyz.accesscontrol.windows.net/v2/wsfederation" realm="http://localhost:3949/" reply="http://localhost:3949/" requireHttps="false" />
<cookieHandler requireSsl="false" path="/" />
</federatedAuthentication>
<issuerNameRegistry type="Microsoft.IdentityModel.Swt.SwtIssuerNameRegistry, Wif.Swt">
<trustedIssuers>
<add name="https://readify.accesscontrol.windows.net/" thumbprint="{thumbprint}" />
</trustedIssuers>
</issuerNameRegistry>
<securityTokenHandlers>
<add type="Microsoft.IdentityModel.Swt.SwtSecurityTokenHandler, Wif.Swt" />
</securityTokenHandlers>
<issuerTokenResolver type="Microsoft.IdentityModel.Swt.SwtIssuerTokenResolver, Wif.Swt" />
</service>
</microsoft.identityModel>

关于c# - WebAuthenticationBroker 是否在 Windows 8 Metro App 发布候选版本中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12485665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com