我用.NET 4.5创建了一个新的ASP.NET MVC应用程序.我已成功使用STS设置身份验证.身份验证流程正常,我可以在Thread.CurrentPrincipal上获取包含所需声明的ClaimsIdentity.
现在我需要引导令牌来保护对我的服务层的调用.我在identityConfiguration元素上将saveBootstrapContext设置为true.
- <system.identityModel>
- <identityConfiguration saveBootstrapContext="true">
但是,ClaimsIdentity上的BootstrapContext属性始终为null.
- var identity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;
- var context = identity.BootstrapContext; // context is always null
我在这里错过了什么吗?这被认为是直截了当的:(
– – – – – – 解决 – – – – – –
重新启动系统后,此问题已得到解决.请注意,在iisreset之后它没有解决.后来我更改了配置以使用Microsoft.IdentityModel而不是System.IdentityModel.我能够重现这种行为.再次重启后,我又能够再次获得引导令牌.
其他人有同样的行为吗?
解决方法
解决了这些:
- <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"]
- }
- }
- );