我一直在按照步骤使Windows 8商店应用获取ACS令牌,如下所述: Does the WebAuthenticationBroker work in Windows 8 Metro App post Release Candidate
Windows 8客户端的身份验证方法
private async void Authenticate()
{
WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri("https://myACSnamespace.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http://localhost:12714/"),
new Uri("http://mypublicIPaddress:80/WebAppMVCAPI/api/federation/end"));
我在Web应用程序上的控制器编程如下:
public class FederationController : ApiController
{
protected virtual string ExtractBootstrapToken()
{
return HttpContext.Current.User.BootstrapToken();
}
[HttpGet]
public string Get()
{
return "Hello Get World";
}
[HttpPost]
public HttpResponseMessage Post()
{
var response = this.Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Add("Location", "/WebAppMVCAPI/api/federation/end?acsToken=" + ExtractBootstrapToken());
return response;
}
}
}
我们的想法是让Windows 8商店应用程序通过Facebook登录从ACS获取令牌。当我启动win8客户端时,该应用程序显示Facebook登录页面。但是,指令return HttpContext.Current.User.Bootstraptoken()
失败,出现以下异常:
NullReferenceException. Object reference not set to an instance of an object.
我的web.config看起来像这样:
<microsoft.identityModel>
<service saveBootstrapTokens="true">
<audienceUris>
<add value="http://localhost:80" />
</audienceUris>
<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" issuer="https://bondsapp.accesscontrol.windows.net/v2/wsfederation" realm="http://localhost:80/" reply="http://localhost:80/" requireHttps="false" />
<cookieHandler requireSsl="false" path="/" />
</federatedAuthentication>
<issuerNameRegistry type="Microsoft.IdentityModel.Swt.SwtIssuerNameRegistry, Wif.Swt">
<trustedIssuers>
<add name="https://bondsapp.accesscontrol.windows.net/" thumbprint="xxxxx" />
</trustedIssuers>
</issuerNameRegistry>
<securityTokenHandlers>
<add type="Microsoft.IdentityModel.Swt.SwtSecurityTokenHandler, Wif.Swt" />
</securityTokenHandlers>
<issuerTokenResolver type="Microsoft.IdentityModel.Swt.SwtIssuerTokenResolver, Wif.Swt" />
</service>
有人可以了解如何使用Bootstraptoken方法获取ACS令牌吗?
由于 路易斯
答案 0 :(得分:0)
我不相信联合身份验证默认设置HttpContext.User。试试
(Thread.CurrentPrincipal as IClaimsPrincipal).Identities[0].BootstrapToken
假设您已经浏览了站点上的令牌处理程序管道(WS-FAM),则应填充此值。这将是一个SecurityToken对象,然后您可以使用正确的SecurityTokenHandler类进行序列化。
答案 1 :(得分:0)
你试过这个:
BootstrapContext bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;
SecurityToken st = bootstrapContext.SecurityToken;