【发布时间】:2015-12-15 11:06:02
【问题描述】:
我从 beta5 迁移到 beta7 ASP.NET vNext,当我尝试使用无效的 JWT 令牌或根本没有令牌访问受保护的 API 控制器时出现以下错误:
InvalidOperationException:以下身份验证方案不是 公认: Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d__10.MoveNext()
如果我尝试使用有效令牌访问受保护的控制器,我可以成功获得响应。
这是我的受保护控制器:
[Authorize]
[Route("api/protected")]
public class ProtectedController : Controller
{
[Route("")]
public IEnumerable<object> Get()
{
var identity = User.Identity as ClaimsIdentity;
return identity.Claims.Select(c => new
{
Type = c.Type,
Value = c.Value
});
}
}
这是我的 Startup 课程:
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
public static IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IApplicationEnvironment env)
{
ConfigureOAuthTokenConsumption(app);
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseErrorPage();
app.UseMvc();
}
private void ConfigureOAuthTokenConsumption(IApplicationBuilder app)
{
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseOwin(addToPipeline =>
{
addToPipeline(next =>
{
var appBuilder = new AppBuilder();
appBuilder.Properties["builder.DefaultApp"] = next;
var issuer = Settings.Issuer;
var audience = Settings.Audience;
var secret = TextEncodings.Base64Url.Decode(Settings.Secret);
appBuilder.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
},
});
return appBuilder.Build<AppFunc>();
});
});
}
}
当我使用 beta5 时,它运行良好。当我请求没有有效令牌的受保护控制器时,我得到了 401 响应,这是正确的行为。 我需要在 ASP.NET vNext beta7 中更改 JWT 令牌消费配置吗?
【问题讨论】:
-
我真的很好奇:既然可以使用 ASP.NET 5 的中间件,为什么还要使用 Katana 的 OAuth2/JWT 不记名身份验证中间件? nuget.org/packages/Microsoft.AspNet.Authentication.OAuthBearer/…
-
@Pinpoint,我尝试使用 app.UseOAuthBearerAuthentication(options => { options.AutomaticAuthentication = true; options.Audience = Audience; options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(secret); }) 使用令牌;但我收到错误'System.InvalidOperationException:IDX10636:SignatureProviderFactory.CreateForVerifying 为密钥返回 null:'System.IdentityModel.Tokens.SymmetricSecurityKey'。在这种情况下,我也不清楚在哪里设置发行人。
-
是的,对称密钥尚不支持。也就是说,您应该真正考虑使用非对称密钥,因为它提供了一种更强大的方法。要设置颁发者,只需使用
options.Issuer = "issuer"。 -
@Pinpoint,我可以看到只有 options.ClaimsIssuer,没有 options.Issuer。这是我需要设置的吗?
-
糟糕,抱歉,我是
options.TokenValidationParameters.ValidIssuer。
标签: visual-studio-2015 asp.net-core asp.net-core-mvc jwt