Windows 8 Bootstraptoken上的NullReferenceException(ACS身份验证)

Windows 8 Bootstraptoken上的NullReferenceException(ACS身份验证),windows-8,wif,windows-store-apps,acs,Windows 8,Wif,Windows Store Apps,Acs,我已按照以下步骤使Windows 8应用商店应用程序获得ACS令牌,如下所述: windows8客户端的身份验证方法 web应用程序上的控制器编程如下: public class FederationController : ApiController { protected virtual string ExtractBootstrapToken() { return HttpContext.Current.User.BootstrapToken();

我已按照以下步骤使Windows 8应用商店应用程序获得ACS令牌,如下所述:

windows8客户端的身份验证方法

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

这个想法是让Windows8商店应用程序通过Facebook登录从ACS获得一个令牌。当我启动win8客户端时,应用程序会显示一个Facebook登录页面。但是,指令返回HttpContext.Current.User.Bootstraptoken失败,出现以下异常:

NullReferenceException。对象引用未设置为对象的实例

我的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令牌吗

谢谢
Luis

我不相信联邦身份验证默认设置了HttpContext.User。试一试

(Thread.CurrentPrincipal as IClaimsPrincipal).Identities[0].BootstrapToken
假设您已经在站点上通过了令牌处理程序管道WS-FAM,那么应该填充它。这将是一个SecurityToken对象,然后您可以使用适当的SecurityTokenHandler类对其进行序列化。

您尝试过以下方法吗:

BootstrapContext bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;                                  
SecurityToken st = bootstrapContext.SecurityToken;
看看维托里奥的帖子:

你好,奥伦,谢谢你的回复。此代码与发布在Windows8工具包的ACS示例上的代码相同。有趣的是,如果我接受这个样本的项目并运行它,它就会工作。但是,我上面发布的代码失败了,出现了上述异常。我假设与下载的示例相比,我的MVC项目中存在一些设置不正确的地方,但我还无法确定是什么…您是否配置了WSFederationAuthenticationModule?没有。我的ID例程使用的是.NET 4.0 Microsoft.identitymodel,但我会看看更新它们是否不会太麻烦。谢谢
BootstrapContext bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;                                  
SecurityToken st = bootstrapContext.SecurityToken;