我正在使用OAuth Bearer令牌构建REST API作为我的身份验证方法。因此,我尝试添加授权策略,以便我可以执行[Authorize("Bearer")]
之类的操作。但是,当我去测试我的新授权策略时,会抛出异常
未接受以下身份验证方案:承载
我试过多次试图阻止这个例外被抛出,但我没有运气。我的启动课程可以在https://gist.github.com/mw2nukeboy/4b6cc7d348ac60336b03找到。
答案 0 :(得分:2)
更新:,不再可以从ConfigureServices
配置安全选项(Identity除外)。您现在需要在调用app.UseJwtBearerAuthentication()
时直接配置JWT选项:
public void Configure(IApplicationBuilder app) {
app.UseJwtBearerAuthentication(options => {
// Configure the JWT options here.
});
}
您忘记在管道中添加OAuth2承载身份验证中间件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseStaticFiles();
app.UseOAuthBearerAuthentication();
app.UseIdentity();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "api/{controller}/{action}/{id?}",
defaults: new {
controller = "Home",
action = "Index"
});
});
}
您还没有使用推荐的方法来注册OAuth2承载中间件使用的设置:
public void ConfigureServices(IServiceCollection services) {
// Not recommended approach.
services.AddInstance(new OAuthBearerAuthenticationOptions { });
// Recommended approach.
services.ConfigureOAuthBearerAuthentication(options => {
// Configure the options used by the OAuth2 bearer middleware.
});
}