我正在使用 OAuth 持有者令牌作为我的身份验证方法构建 REST API。因此,我尝试添加一个授权策略,以便我可以执行类似 [授权(“持有者”)]
的操作。但是,当我去测试我的新授权策略时,会抛出一个异常,指出
未接受以下身份验证方案:承载
我尝试了多种方法试图阻止这个异常被抛出,但是我没有任何运气。我的启动类可以在https://gist.github.com/mw2nukeboy/4b6cc7d348ac60336b03.找到
更新:在最近的测试版中,不再可以从ConfigreServices
配置安全选项(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.
});
}