-->

BootstrapContext是ClaimsIdentity空(BootstrapContext

2019-08-05 01:01发布

我已创建了.NET 4.5的新ASP.NET MVC应用程序。 我已经成功地建立了认证与STS。 认证流程是工作的罚款,我能够得到ClaimsIdentity,包含所需的权利要求,在Thread.CurrentPrincipal中。

现在我需要引导标志,以确保我的服务层的调用。 我已经设置了saveBootstrapContext为true identityConfiguration元素。

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

然而,在ClaimsIdentity的BootstrapContext属性始终为空。

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

我错过了什么吗? 这应该是简单:(

- - - - - - 解决 - - - - - -

之后我重新启动我的系统,此问题已得到解决。 需要注意的是它的一个IISRESET后没有得到解决。 后来,我改变了配置中使用Microsoft.IdentityModel而不是System.IdentityModel。 我能摄制此行为。 另一个重新启动后,我能再次获得引导标记。 任何人都经历了同样的行为?

Answer 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"]
        }
    }
);


Answer 2:

在IIS快递托管时,我就遇到了这个问题。 事实证明,这个问题是我的浏览器 - 我没有关闭了我所有的浏览器窗口或清除饼干,所以SessionSecurityToken没有被新设置重建,即使服务器已经重启(现有FedAuth的cookie仍然还在从浏览器发送)。

有一次,我不得不重新认证通过关闭所有浏览器窗口,重新启动浏览器并再次执行我的要求,BootstrapContext存在。



Answer 3:

如果您使用的是消息处理程序使用手工验证令牌JwtSecurityTokenHandler提取索赔本金并附加到当前线程,如这里所描述使用JWT处理程序,实现“穷人”的授权/ ACTAS ,当您正在使用验证令牌JwtSecurityTokenHandler.ValidateToken()就设置一个TokenValidationParametersSaveBootstrapContext ,设置, true做的伎俩。



文章来源: BootstrapContext is null on ClaimsIdentity