相关疑难解决方法(0)

具有ASP.NET Core身份的Web API的IdentityServer4基于角色的授权

我正在将IdentityServer4与.Net Core 2.1和Asp.Net Core标识一起使用。我的解决方案中有两个项目。

  • 身份服务器
  • 网络API

我想保护我的Web API,我使用邮差请求新的令牌,它可以正常工作并且令牌已成功生成。当我[Authorize]在没有角色的控制器上使用时 ,它可以完美工作,但是当我使用[Authorize(Roles="Student")](甚至与[Authorize(Policy="Student")])一起使用时 ,它总是返回403 forbidden

完整代码的Github链接:https : //github.com/Mohammad7070/ShagerdanehIdentityServer

我的代码有什么问题

Web API startup.cs

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

        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)
        {



            services.AddMvcCore()
            .AddAuthorization(options => options.AddPolicy("Student", policy => policy.RequireClaim("Role", "Student")))
            .AddAuthorization(options => options.AddPolicy("Teacher", policy => policy.RequireClaim("Role", …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-identity asp.net-core identityserver4

6
推荐指数
2
解决办法
4030
查看次数