在ASP.NET Core Razor中的授权

huangapple go评论54阅读模式
英文:

Authorization in ASP.NET Core razor

问题

我有一个管理员区域,我希望除了普通用户之外的每个用户都能访问管理员区域。

我的用户类型是动态的 - 我应该怎么办?

我的身份验证是基于权限的。

我使用了这段代码,但它是针对静态帐户类型的:

services.AddAuthorization(options =>
{
    builder => builder.RequireRole(Roles.Administrator, Roles.ContentUploader));
    options.AddPolicy("Discount",
        builder => builder.RequireRole(Roles.Administrator));
});
英文:

I have an admin area I want every user except normal user to be able o go to the admin area.

My user types are dynamic - what should I do?

My authentication is permission based.

I used this code, but it is for static account types:

services.AddAuthorization(options =>
        {
            builder => builder.RequireRole(Roles.Administrator, Roles.ContentUploader));
            options.AddPolicy("Discount",
                builder => builder.RequireRole(Roles.Administrator));
        });

答案1

得分: 0

根据您的描述,我建议您可以编写一个自定义策略,如下所示,以满足您的要求。

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("NonNormalUser", policy =>
        policy.RequireAssertion(context =>
            !context.User.IsInRole("Normal")));
});
英文:

According to your description, I suggest you could write a custom policy like below to match your requirement.

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("NonNormalUser", policy =>
        policy.RequireAssertion(context =>
            !context.User.IsInRole("Normal")));
});

huangapple
  • 本文由 发表于 2023年2月18日 03:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定