asp.net-core - 无法在 AuthorizeView 中获取用户授权状态?
问题描述
在我的剃须刀组件中,我有这个 AuthorizeView:
<AuthorizeView Roles="Administrator">
<Authorized>
<span>Authorized @context.User.IsInRole("Administrator")</span>
</Authorized>
<NotAuthorized>
<span>Not Authorized @context.User.IsInRole("Administrator")</span>
</NotAuthorized>
</AuthorizeView>
我也试图把所有的角色都放在那里,但无济于事:
<AuthorizeView Roles="Administrator,Supervisor,Operator,Readonly">
当我使用具有管理员角色的用户登录时,用户角色似乎不是管理员。@context.User.IsInRole("Administrator") 将始终返回 False。我正在构建的这个应用程序,一个用户只能拥有一个角色。我在这里错过了什么吗?我需要在 Program.cs 中添加任何内容吗?这是 Program.cs 文件:
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#bwapp");
builder.Services.AddHttpClient("BWebApp.ServerAPI", (sp, client) =>
{
client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
client.EnableIntercept(sp);
})
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BWebApp.ServerAPI"));
builder.Services.AddHttpClientInterceptor();
builder.Services.AddScoped<HttpInterceptorService>();
builder.Services.AddTransient(typeof(IHttpRepository<>), typeof(HttpRepository<>));
builder.Services.AddApiAuthorization();
await builder.Build().RunAsync();
}
}
这是 startup.cs 代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
Configuration.GetConnectionString("Connection")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("Connection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddControllersWithViews().AddNewtonsoftJson(
op => op.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddRazorPages();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
解决方案
这可能是因为用户角色表没有该用户与管理员角色之间的关联。或者角色表中不存在管理员角色。
推荐阅读
- ruby-on-rails - Ruby on Rails 和 Sidekiq redis 连接池与并行测试?
- google-sheets - 在 Google 表格中使用 IMPORTHTML 或 IMPORTXML 导入可扩展表格的所有行
- c++ - 带有restc-cpp发布数据断言错误的Rest API
- python - 无法使用 python 代码从存储容器上传/下载文件
- python - 在新值上重置的累积和,没有循环
- python - PyTelegramBotApi - 如果其他功能正在运行,如何停止一个功能?
- go - 如何惯用地检查接口是否是 Go 中的两种类型之一
- python - 索引值和列标题值之间的 Pandas 矩阵加法/除法
- swift - 是否可以使用 UserDefaults 在根首选项中编写 userDefaults?
- javascript - 在存在未保存的更改错误消息中单击确定按钮时,APEX 交互式网格变得空白。如何避免这种情况?