首页 > 解决方案 > 将用户分配给 asp.net core 中的角色将返回此错误“您无权访问此资源”。

问题描述

我创建了一个使用个人用户帐户的新 asp.net 核心 Web 应用程序。现在我正在尝试实现一个简单的角色分配方案。

所以我注册了一个测试用户,该用户被添加到 AspNetUser 表中:- 在此处输入图像描述

然后我在 AspNetRole 中添加一个名为“Administrator”的新角色:- 在此处输入图像描述

然后我添加了一个新的 AspNetUserRole 来将用户链接到角色:- 在此处输入图像描述

然后我在 About 操作方法上添加了以下 Authorize 注释:-

[Authorize(Roles = "Administrator")]
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

但是当我尝试使用用户访问 About 操作方法时,我收到了这个错误:-

您无权访问此资源。”

编辑

这是startup.cs我没有修改过的,所以我认为它包含内置代码:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication2.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication2
{
    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.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

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

标签: asp.net-coreasp.net-core-mvcasp.net-identity

解决方案


AspNetUserRole我猜您在创建 user 后手动在 table 中创建角色和链接角色。请不要忘记注销用户并再次登录,因此角色声明将获取/更新新添加的角色。


推荐阅读