c# – InvalidOperationException:找不到名为:’Bearer’的AuthorizationPolicy
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – InvalidOperationException:找不到名为:’Bearer’的AuthorizationPolicy,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2190字,纯文字阅读大概需要4分钟。
内容图文
![c# – InvalidOperationException:找不到名为:’Bearer’的AuthorizationPolicy](/upload/InfoBanner/zyjiaocheng/754/2e9c45a5d6f143e6817e3550ebac0688.jpg)
我目前正在尝试学习如何使用持票令牌构建安全api,我不断收到此错误(InvalidOperationException:未找到名为’Bearer’的AuthorizationPolicy.)我不知道为什么.我正在使用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?}");
});
}
}
解决方法:
我没有使用策略,当我忘记在authorize属性中指出角色时,这个错误发生在我身上.
我有这个:
[Authorize("Administrator")] // if you don't specify the property name Roles it will consider it as the policy name
通过将其更改为:
[Authorize(Roles = "Administrator")]
内容总结
以上是互联网集市为您收集整理的c# – InvalidOperationException:找不到名为:’Bearer’的AuthorizationPolicy全部内容,希望文章能够帮你解决c# – InvalidOperationException:找不到名为:’Bearer’的AuthorizationPolicy所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。