【问题标题】:NullReferenceException on Bootstraptoken (ACS authentication)Bootstraptoken 上的 NullReferenceException(ACS 身份验证)
【发布时间】:2013-01-05 21:19:11
【问题描述】:

我一直按照以下步骤使 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"));

我在网络应用程序上的控制器编程如下:

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 令牌吗?

谢谢 路易斯

【问题讨论】:

    标签: windows-8 wif windows-store-apps acs


    【解决方案1】:

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

    (Thread.CurrentPrincipal as IClaimsPrincipal).Identities[0].BootstrapToken
    

    假设您已通过站点上的令牌处理程序管道 (WS-FAM),应该填充此内容。这将是一个 SecurityToken 对象,然后您可以使用适当的 SecurityTokenHandler 类对其进行序列化。

    【讨论】:

    • 嗨,奥伦,感谢您的回复。此代码与 Windows 8 Toolkit 的 ACS 示例中发布的代码相同。有趣的是,如果我采用该示例的项目并运行它,它就可以工作。但是,我的代码(如上所述)因上述异常而失败。我假设我的 MVC 项目与下载的示例有一些不正确的设置,但我无法弄清楚是什么......
    • 您是否配置了 WSFederationAuthenticationModule? msdn.microsoft.com/en-us/library/…
    【解决方案2】:

    你试过了吗:

    BootstrapContext bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;                                  
    SecurityToken st = bootstrapContext.SecurityToken;
    

    看看 Vittorio 的帖子: http://blogs.msdn.com/b/vbertocci/archive/2012/11/30/using-the-bootstrapcontext-property-in-net-4-5.aspx

    【讨论】:

    • 不,我没有。我的 ID 例程使用的是 .NET 4.0 (Microsoft.identitymodel),但我会看看更新它们是否不会太麻烦。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2014-06-06
    • 1970-01-01
    • 2013-09-13
    • 2016-03-24
    • 2023-04-01
    相关资源
    最近更新 更多