我试图让JwtBearerAuthentication在ASP.Net 5,MVC6 WebApi应用程序中工作。我在令牌端点生成了Jwt令牌,但是当我尝试使用该令牌访问所有WebApi端点时,我得到了以下错误:
System.InvalidOperationException: IDX10803:无法从:IDX10803获得配置
这是我的Startup.cs中的代码,有人能告诉我我做错了什么吗?
public Startup(IHostingEnvironment env)
{
const string _TokenIssuer = "http://localhost:5000/";
const string _TokenAudience = "http://localhost:5000/";
public void ConfigureServices(IServiceCollection services)
{
...
RsaSecurityKey _signingKey = null;
SigningCredentials signingCredentials = null;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
{
signingKey = new RsaSecurityKey(rsa.ExportParameters(true));
signingCredentials = new SigningCredentials(_SigningKey, SecurityAlgorithms.RsaSha256Signature);
}
services.AddInstance<SigningCredentials>(signingCredentials); // tobe used in JwtTokenHandler in the Token Controller
var jwtOptions = new JwtBearerOptions();
jwtOptions.AutomaticAuthenticate = true;
jwtOptions.TokenValidationParameters.IssuerSigningKey = signingKey;
jwtOptions.TokenValidationParameters.ValidAudience = _TokenAudience;
jwtOptions.TokenValidationParameters.ValidIssuer = _TokenIssuer;
services.AddInstance<JwtBearerOptions>(jwtOptions); //tobe used in the Token Controller
services.AddAuthorization(auth =>
{
auth.AddPolicy(JwtBearerDefaults.AuthenticationScheme, new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
services.AddMvc();
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
//Add Bearer Authentication Middleware, make sure this is before UseIdentity line
app.UseJwtBearerAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.Audience = _TokenAudience;
options.Authority = _TokenIssuer;
options.RequireHttpsMetadata = false;
});
app.UseIdentity();
app.UseMvc();
}
}
我用Kestrel来主持,我的服务网址是http://localhost:5000/XXX
编辑:--在验证令牌时,JwtBearerAuthentication仍然有问题。我切换到使用OpenIdConnectServer来发出令牌。那部分没问题。但是,当尝试使用JwtBearerAuthentication验证令牌时,除非我设置了options.TokenValidationParameters.ValidateAudience = false
,否则总是得到SecurityTokenInvalidAudienceException: IDX10208: Unable to validate audience. validationParameters.ValidAudience is null or whitespace and validationParameters.ValidAudiences is null
。
当我将其设置为false时,我可以按下我的WebApi代码,但是,当我检查控制器中的User
对象时,没有设置User.Identity.Name
。User.Identity.Authenticationtype="Authentication.Federation"
和User.Identity.IsAuthenticated = true
.我可以看到标识下的声明列表,但并不是我添加到令牌中的所有声明都在那里。这与我在.Net 4.5中使用UseOAuthBearerAuthentication时看到的情况确实不同。
我把我的测试项目放在了GitHub https://github.com/Sally-Xu/Net5Start。你能看出是怎么回事吗?
发布于 2015-12-19 15:18:51
您所看到的错误是由您将Authority
设置为非空值造成的。只有当令牌由OpenID连接服务器(如AspNet.Security.OpenIdConnect.Server )发出时,才应使用此属性。由于您使用的是自定义令牌控制器,所以我猜它不是真正的OAuth2/OpenID服务器,这解释了为什么您会看到这个错误。
停止设置此属性,它应该可以工作。请注意,您必须在IssuerSigningKey
调用UseJwtBearerAuthentication
中直接设置ConfigureServices
。
https://stackoverflow.com/questions/34344860
复制相似问题