【问题标题】:BootstrapContext is null on ClaimsIdentityBootstrapContext 在 ClaimsIdentity 上为空
【发布时间】:2012-12-14 13:37:01
【问题描述】:

我使用 .NET 4.5 创建了一个新的 ASP.NET MVC 应用程序。我已成功使用 STS 设置身份验证。身份验证流程运行良好,我能够在 Thread.CurrentPrincipal 上获取包含所需声明的 ClaimsIdentity。

现在我需要引导令牌来保护对我的服务层的调用。我已在 identityConfiguration 元素上将 saveBootstrapContext 设置为 true。

<system.identityModel>
    <identityConfiguration saveBootstrapContext="true">

但是,ClaimsIdentity 上的 BootstrapContext 属性始终为空。

var identity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;
var context = identity.BootstrapContext; // context is always null

我在这里遗漏了什么吗?这应该很简单:(

【问题讨论】:

  • 试试identity = ClaimsPrincipal.Current.Identities.First() as ClaimsIdentity
  • 与 ClaimsPrincipal.Current.Identities.First() 的结果相同。它是 Claimsidentity 的同一个实例。
  • 您是如何设置外部身份验证的?使用 WS-Federation 身份验证模块?
  • 你让它工作了吗?我添加了标志,但引导上下文仍然为空。

标签: asp.net-mvc .net-4.5 wif claims-based-identity ws-federation


【解决方案1】:

通过以下方式解决:

<system.identityModel>
    <identityConfiguration saveBootstrapContext="true" />
</system.identityModel>

还需要设置TokenValidationParameters.SaveSigninToken区别于JwtBearerOptions.SaveTokens

app.UseWindowsAzureActiveDirectoryBearerAuthentication(
    new WindowsAzureActiveDirectoryBearerAuthenticationOptions {
        Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
        TokenValidationParameters = new TokenValidationParameters {
            SaveSigninToken = true,               
            ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
        }
    }
);

【讨论】:

  • 这真的很有帮助!它解决了我长期以来的难题。
  • 网络配置元素对我没有任何作用,但SaveSigninToken = true 对我来说很关键。谢谢!
【解决方案2】:

我在 IIS Express 中托管时遇到了这个问题。事实证明问题出在我的浏览器上——我没有关闭所有浏览器窗口或清除 cookie,因此即使服务器已重新启动(现有的 FedAuth cookie 仍在从浏览器发送)。

一旦我通过关闭所有浏览器窗口、重新启动浏览器并再次执行我的请求来强制重新进行身份验证,就会出现 BootstrapContext。

【讨论】:

    【解决方案3】:

    如果您使用消息处理程序手动验证令牌,使用 JwtSecurityTokenHandler 提取声明主体并将其附加到当前线程,如Using the JWT handler for Implementing “Poor Man”’s Delegation/ActAs 中所述,当您使用验证令牌时JwtSecurityTokenHandler.ValidateToken()TokenValidationParameters 上的设置之一是 SaveBootstrapContext,设置 true 就可以了。

    【讨论】:

    • 我正在使用 System.IdentityModel.Tokens.Jwt,Version=4.0,但它不包含 SaveBootstrapContext 任何想法?
    • @Homam 我有同样的问题并验证TokenValidationParameters.SaveToken = true填充了BootstrapContext
    【解决方案4】:

    我使用的是 Microsoft.AspNetCore.Authentication.OpenIdConnect,版本=5.0.4.0,设置是这样的:

    .AddOpenIdConnect(o =>
    {
        // . . .
        o.TokenValidationParameters.SaveSigninToken = true;
    })
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2021-04-08
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多