向ASP.NET5应用程序添加OAuth承载授权策略的问题
我正在构建一个RESTAPI,使用OAuth承载令牌作为身份验证方法。因此,我尝试添加一个授权策略,以便可以执行类似于向ASP.NET5应用程序添加OAuth承载授权策略的问题,asp.net,oauth,asp.net-core,Asp.net,Oauth,Asp.net Core,我正在构建一个RESTAPI,使用OAuth承载令牌作为身份验证方法。因此,我尝试添加一个授权策略,以便可以执行类似于[授权(“持有人”)]的操作。但是,当我去测试我的新授权策略时,会抛出一个异常 未接受以下身份验证方案:承载 我尝试了多种方法试图阻止抛出此异常,但我没有任何运气。我的启动类可以在以下位置找到。更新:在最近的Beta中,不再可以从ConfigureServices配置安全选项(身份除外)。您现在需要在调用app.useJWTBeareAuthentication()时直接配置JW
[授权(“持有人”)]
的操作。但是,当我去测试我的新授权策略时,会抛出一个异常
未接受以下身份验证方案:承载
我尝试了多种方法试图阻止抛出此异常,但我没有任何运气。我的启动类可以在以下位置找到。更新:在最近的Beta中,不再可以从
ConfigureServices
配置安全选项(身份除外)。您现在需要在调用app.useJWTBeareAuthentication()时直接配置JWT选项。
:
您忘记在管道中添加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.
});
}
现在,它无法解析OAuthBeareAuthentication选项服务
configureOAuthBeareAuthentication
位于Microsoft.Framework.DependencyInjection
命名空间中。请注意,您也可以在app.useAuthBeareAuthentication(选项=>{…})中直接配置中间件选项
调用。我无法解析OAuthBeareAuthentication,IAApplicationBuider不包含它。名称空间也是Microsoft.Extensions.DependencyInjection。如果您使用的是最新版本,这并不奇怪,因为这个答案是在7月份写的:app.UseAuthBeareAuthentication()现在是app.UseJWTBeareAuthentication()(您可以在Microsoft.AspNet.Authentication.JWTBeareAuthentication包中找到它)Microsoft.Framework命名空间现在是Microsoft.Extensions。我会更新我的答案。
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.
});
}