【问题标题】:InvalidOperationException: The AuthorizationPolicy named: 'Bearer' was not foundInvalidOperationException:未找到名为“Bearer”的 AuthorizationPolicy
【发布时间】:2018-09-17 22:12:07
【问题描述】:

我目前正在尝试学习如何使用不记名令牌构建安全 api,但我不断收到此错误(InvalidOperationException: The AuthorizationPolicy named: 'Bearer' was not found.),我不知道为什么。我正在使用 asp.net-core 2.0 并尝试使用 jwt auth 中间件。 这是我的启动课程,任何帮助将不胜感激!

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    const string TokenAudience = "ExampleAudience";
    const string TokenIssuer = "ExampleIssuer";
    private RsaSecurityKey key;
    private TokenAuthOptions tokenOptions;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        var keyParams = RSAKeyUtils.GetRandomKey();
        key = new RsaSecurityKey(keyParams);

        tokenOptions = new TokenAuthOptions()
        {
            Audience = TokenAudience,
            Issuer = TokenIssuer,
            SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
        };

        services.AddDbContext<VulnerabilityContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddScoped<LoggingActionFilter>();
        services.AddScoped<VulnsService>();

        services.AddAuthentication(o =>
        {
            o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(o =>
        {
            o.Authority = "https://localhost:54302";
            o.Audience = tokenOptions.Audience;
            o.RequireHttpsMetadata = false;
        });

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        //app.UseSession();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseAuthentication();

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

【问题讨论】:

    标签: c# authorization asp.net-core-2.0 bearer-token


    【解决方案1】:

    您收到此错误是因为身份验证方案和授权策略不是一回事。让我们看看它们分别是什么。

    身份验证方案

    它们是您应用程序中不同的身份验证方法。在您发布的代码中,您有一个身份验证方案,由名称 Bearer 和您指定的选项标识。

    可以在一个应用程序中设置多个身份验证方案:

    • 您可以使用 cookie 或 JWT 不记名令牌身份验证对用户进行身份验证
    • 您甚至可以接受来自不同来源的 JWT 令牌;在这种情况下,您需要调用AddJwtBearer 方法两次。同样重要的是要注意身份验证方案的名称应该是唯一的,因此您需要使用overload that takes the name and the options configuration delegate

    授权政策

    当用户在您的应用程序中通过身份验证时,这并不意味着它可以访问其中的每一项功能。您可能拥有不同的访问级别,其中 管理员 拥有其他人没有的特殊权限;这在 ASP.NET Core 中使用授权策略表示。我强烈建议您阅读official documentation on authorization,因为我认为它很棒。

    授权策略由两部分组成:

    • 独一无二的名字
    • 一组要求

    以上面提到的管理员为例,我们可以创建一个虚构的授权策略:

    • 姓名:Administrators
    • 要求:必须经过身份验证并具有 role 声明和 Administrators

    这将在代码中以这种方式表示:

    services.AddAuthorization(options =>
    {
        options.AddPolicy("Administrators", new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .RequireClaim("role", "Administrators")
            .Build());
    });
    

    然后,您可以通过使用[Authorize(Policy = "Administrators")] 属性装饰应用程序中的某些特定控制器或操作,将此策略应用于它们。然后,MVC 将在请求期间针对当前用户运行需求,并确定他们是否可以访问特定功能。

    我的猜测是您在其中一个操作/控制器上添加了这样一个属性,但您没有在授权系统中注册授权策略名称Bearer

    如果您的目标是阻止未经身份验证的用户访问某些操作,您可以应用 [Authorize] 属性。这样做会运行default policy,默认情况下,它只需要对用户进行身份验证。

    【讨论】:

      【解决方案2】:

      我没有使用策略,当我忘记在授权属性中指明角色时,这个错误发生在我身上。

      我有这个:

      [Authorize("Administrator")] // if you don't specify the property name Roles it will consider it as the policy name
      

      将其更改为:

      [Authorize(Roles = "Administrator")]
      

      【讨论】:

        【解决方案3】:

        AuthenticationSchemes 添加到控制器类对我有用:

        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        

        【讨论】:

          猜你喜欢
          • 2019-03-12
          • 1970-01-01
          • 1970-01-01
          • 2018-06-21
          • 2020-10-08
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 2020-08-25
          相关资源
          最近更新 更多