我正在尝试让 JwtBearerAuthentication 在 ASP.Net 5、MVC6 WebApi 应用程序中工作。我在令牌端点生成了 Jwt 令牌,但是当我尝试使用该令牌访问所有 WebApi 终结点时,出现以下错误:
系统无效操作异常: IDX10803: 无法获取配置 发件人:localhost:5000/.well-known/openid-configuration。
这是我的创业.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
编辑:我仍然在使用 JwtBearer身份验证来验证令牌时遇到问题。我改用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 中。你能看到哪里出了问题吗?
看到的错误是由您将Authority
设置为非空值引起的。仅当令牌由 OpenID Connect 服务器(如 AspNet.Security.OpenIdConnect.Server(颁发时,才应使用此属性。由于您使用的是自定义令牌控制器,因此我想它不是真正的OAuth2/OpenID Connect服务器,这解释了为什么您会看到此错误。
停止设置此属性,它应该可以工作。请注意,您必须直接在UseJwtBearerAuthentication
调用中设置IssuerSigningKey
,因为它不会重用ConfigureServices
中设置的选项。