I'm trying to get JwtBearerAuthentication working in an ASP.Net 5, MVC6 WebApi application. I got the Jwt token generated at my token end point, but when I try to use the token to get access all the WebApi endpoint, I got the following error:
System.InvalidOperationException: IDX10803: Unable to obtain configuration from: localhost:5000/.well-known/openid-configuration.
Here is the code in my Startup.cs, can any one tell me what I did wrong.
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();
}
}
I'm using Kestrel to host and my service url is http://localhost:5000/XXX
Edit: I'm still having problems with JwtBearerAuthentication to validate token. I switched to use OpenIdConnectServer to issue token. That part is fine. But when try to validate token using JwtBearerAuthentication, unless I set options.TokenValidationParameters.ValidateAudience = false
; I always get SecurityTokenInvalidAudienceException: IDX10208: Unable to validate audience. validationParameters.ValidAudience is null or whitespace and validationParameters.ValidAudiences is null
.
When I set it to false, I can hit my WebApi code, however, when I inspect the User
object in the controller, the User.Identity.Name
is not set. User.Identity.Authenticationtype="Authentication.Federation"
and User.Identity.IsAuthenticated = true
. I can see a list of Claims under the Identity, but not all the claims I added to the token are there. This is really different than what I have seen in .Net 4.5 with UseOAuthBearerAuthentication.
I have put my testing project in GitHub https://github.com/Sally-Xu/Net5Start. Could you see what is wrong?