使用 ASP.Net 5 和 ASP.NET MVC 6 的 JwtBearerAuthentication
JwtBearerAuthentication with ASP.Net 5 and ASP.NET MVC 6
我正在尝试让 JwtBearerAuthentication 在 ASP.Net 5、MVC6 WebApi 应用程序中工作。我在令牌端点生成了 Jwt 令牌,但是当我尝试使用令牌访问所有 WebApi 端点时,出现以下错误:
System.InvalidOperationException: IDX10803: 无法获取配置
来自:localhost:5000/.well-known/openid-configuration.
这是我的 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 进行托管,我的服务 url 是 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
。我可以在身份下看到一个声明列表,但并不是我添加到令牌中的所有声明都在那里。这与我在带有 UseOAuthBearerAuthentication 的 .Net 4.5 中看到的完全不同。
我已经把我的测试项目放在GitHubhttps://github.com/Sally-Xu/Net5Start中了。你能看出哪里出了问题吗?
您看到的错误是由于您将 Authority
设置为非空值造成的。此 属性 仅应在您的令牌由 OpenID Connect 服务器(如 AspNet.Security.OpenIdConnect.Server)颁发时使用。由于您使用的是自定义令牌控制器,我猜它不是真正的 OAuth2/OpenID 连接服务器,这就解释了为什么您会看到此错误。
停止设置这个 属性 它应该可以工作。请注意,您必须直接在 UseJwtBearerAuthentication
调用 .
中设置 IssuerSigningKey
我正在尝试让 JwtBearerAuthentication 在 ASP.Net 5、MVC6 WebApi 应用程序中工作。我在令牌端点生成了 Jwt 令牌,但是当我尝试使用令牌访问所有 WebApi 端点时,出现以下错误:
System.InvalidOperationException: IDX10803: 无法获取配置 来自:localhost:5000/.well-known/openid-configuration.
这是我的 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 进行托管,我的服务 url 是 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
。我可以在身份下看到一个声明列表,但并不是我添加到令牌中的所有声明都在那里。这与我在带有 UseOAuthBearerAuthentication 的 .Net 4.5 中看到的完全不同。
我已经把我的测试项目放在GitHubhttps://github.com/Sally-Xu/Net5Start中了。你能看出哪里出了问题吗?
您看到的错误是由于您将 Authority
设置为非空值造成的。此 属性 仅应在您的令牌由 OpenID Connect 服务器(如 AspNet.Security.OpenIdConnect.Server)颁发时使用。由于您使用的是自定义令牌控制器,我猜它不是真正的 OAuth2/OpenID 连接服务器,这就解释了为什么您会看到此错误。
停止设置这个 属性 它应该可以工作。请注意,您必须直接在 UseJwtBearerAuthentication
调用
IssuerSigningKey