如何在asp.net核心中创建角色并将其分配给用户

Bip*_*aul 52 c# asp.net asp.net-mvc asp.net-identity-3 asp.net-core

我使用asp.net核心默认网站模板和身份验证选择为个人用户帐户.如何创建角色并将其分配给用户,以便我可以使用控制器中的角色来过滤访问权限.

Tem*_*oke 65

我的评论被删除了,因为我提供了一个链接,我在这里回答了类似的问题.Ergo,这次我会更具描述性地回答.开始.

您可以通过CreateRolesstartup班级中创建方法轻松完成此操作.这有助于检查角色是否已创建,如果不是,则创建角色; 在应用程序启动.像这样.

private async Task CreateRoles(IServiceProvider serviceProvider)
    {
        //initializing custom roles 
        var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        string[] roleNames = { "Admin", "Manager", "Member" };
        IdentityResult roleResult;

        foreach (var roleName in roleNames)
        {
            var roleExist = await RoleManager.RoleExistsAsync(roleName);
            if (!roleExist)
            {
                //create the roles and seed them to the database: Question 1
                roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
            }
        }

        //Here you could create a super user who will maintain the web app
        var poweruser = new ApplicationUser
        {

            UserName = Configuration["AppSettings:UserName"],
            Email = Configuration["AppSettings:UserEmail"],
        };
    //Ensure you have these values in your appsettings.json file
        string userPWD = Configuration["AppSettings:UserPassword"];
        var _user = await UserManager.FindByEmailAsync(Configuration["AppSettings:AdminUserEmail"]);

       if(_user == null)
       {
            var createPowerUser = await UserManager.CreateAsync(poweruser, userPWD);
            if (createPowerUser.Succeeded)
            {
                //here we tie the new user to the role
                await UserManager.AddToRoleAsync(poweruser, "Admin");

            }
       }
    }
Run Code Online (Sandbox Code Playgroud)

然后你可以CreateRoles(serviceProvider).Wait();ConfigureStartup类中的方法调用该方法.确保您拥有该类中IServiceProvider的参数Configure.

在控制器中使用基于角色的授权来过滤用户访问:问题2

你可以这样轻松地做到这一点.

[Authorize(Roles="Manager")]
public class ManageController : Controller
{
   //....
}
Run Code Online (Sandbox Code Playgroud)

您也可以在动作方法中使用基于角色的授权.如果愿意,可以分配多个角色

[Authorize(Roles="Admin, Manager")]
public IActionResult Index()
{
/*
 .....
 */ 
}
Run Code Online (Sandbox Code Playgroud)

虽然这样做很好,但为了更好的练习,您可能希望阅读有关使用基于策略的角色检查的信息.你可以在这里的ASP.NET核心文档中找到它,或者我在这里写的关于它的这篇文章

  • @mahmoudfathy将`services.AddDefaultIdentity &lt;ApplicationUser&gt;()。AddRoles &lt;IdentityRole&gt;()。AddEntityFrameworkStores &lt;DbContext&gt;();`添加到您的`ConfigureServices`中 (4认同)
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。- [来自评论](/review/low-quality-posts/15347488) (2认同)
  • 谢谢让。我按照建议将代码的基本部分包括在答案中,并提供了供参考的链接。 (2认同)

Ste*_*iez 43

我在帐户控制器中创建了一个操作,该操作调用一个函数来创建角色并将管理员角色影响到默认用户(您应该删除生产中的默认用户):

    private async Task createRolesandUsers()
    {  
        bool x = await _roleManager.RoleExistsAsync("Admin");
        if (!x)
        {
            // first we create Admin rool    
            var role = new IdentityRole();
            role.Name = "Admin";
            await _roleManager.CreateAsync(role);

            //Here we create a Admin super user who will maintain the website                   

            var user = new ApplicationUser();
            user.UserName = "default";
            user.Email = "default@default.com";

            string userPWD = "somepassword";

            IdentityResult chkUser = await _userManager.CreateAsync(user, userPWD);

            //Add default User to Role Admin    
            if (chkUser.Succeeded)
            {
                var result1 = await _userManager.AddToRoleAsync(user, "Admin");
            }
        }

        // creating Creating Manager role     
        x = await _roleManager.RoleExistsAsync("Manager");
        if (!x)
        {
            var role = new IdentityRole();
            role.Name = "Manager";
            await _roleManager.CreateAsync(role);
        }

        // creating Creating Employee role     
        x = await _roleManager.RoleExistsAsync("Employee");
        if (!x)
        {
            var role = new IdentityRole();
            role.Name = "Employee";
            await _roleManager.CreateAsync(role);
        }
  }
Run Code Online (Sandbox Code Playgroud)

之后,您可以创建一个控制器来管理用户的角色.

  • _roleManager是什么类型的? (3认同)

Pau*_*son 20

Temi的答案几乎是正确的,但你不能像他建议的那样用非异步函数调用异步函数.你需要做的是在同步函数中进行异步调用,如下所示:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

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

        CreateRoles(serviceProvider);

    }

    private void CreateRoles(IServiceProvider serviceProvider)
    {

        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        Task<IdentityResult> roleResult;
        string email = "someone@somewhere.com";

        //Check that there is an Administrator role and create if not
        Task<bool> hasAdminRole = roleManager.RoleExistsAsync("Administrator");
        hasAdminRole.Wait();

        if (!hasAdminRole.Result)
        {
            roleResult = roleManager.CreateAsync(new IdentityRole("Administrator"));
            roleResult.Wait();
        }

        //Check if the admin user exists and create it if not
        //Add to the Administrator role

        Task<ApplicationUser> testUser = userManager.FindByEmailAsync(email);
        testUser.Wait();

        if (testUser.Result == null)
        {
            ApplicationUser administrator = new ApplicationUser();
            administrator.Email = email;
            administrator.UserName = email;

            Task<IdentityResult> newUser = userManager.CreateAsync(administrator, "_AStrongP@ssword!");
            newUser.Wait();

            if (newUser.Result.Succeeded)
            {
                Task<IdentityResult> newUserRole = userManager.AddToRoleAsync(administrator, "Administrator");
                newUserRole.Wait();
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

关键是使用Task <>类并强制系统以同步方式稍微不同地等待.

  • 为什么不将其放在主程序可以是异步的程序中,并且我们使用角色配置作为 IWebHost 扩展。这似乎是一些微软建议的(扩展,而不是主要的异步):https://learn.microsoft.com/en-us/archive/msdn-magazine/2019/april/data-points-ef-core-在 docker 容器化应用程序中 (2认同)

har*_*eyt 7

我用这个(DI):

public class IdentitySeed
{
    private readonly ApplicationDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<ApplicationRole> _rolesManager;
    private readonly ILogger _logger;

    public IdentitySeed(
        ApplicationDbContext context,
        UserManager<ApplicationUser> userManager,
        RoleManager<ApplicationRole> roleManager,
         ILoggerFactory loggerFactory) {
        _context = context;
        _userManager = userManager;
        _rolesManager = roleManager;
        _logger = loggerFactory.CreateLogger<IdentitySeed>();
    }

    public async Task CreateRoles() {
        if (await _context.Roles.AnyAsync()) {// not waste time
            _logger.LogInformation("Exists Roles.");
            return;
        }
        var adminRole = "Admin";
        var roleNames = new String[] { adminRole, "Manager", "Crew", "Guest", "Designer" };

        foreach (var roleName in roleNames) {
            var role = await _rolesManager.RoleExistsAsync(roleName);
            if (!role) {
                var result = await _rolesManager.CreateAsync(new ApplicationRole { Name = roleName });
                //
                _logger.LogInformation("Create {0}: {1}", roleName, result.Succeeded);
            }
        }
        // administrator
        var user = new ApplicationUser {
            UserName = "Administrator",
            Email = "something@something.com",
            EmailConfirmed = true
        };
        var i = await _userManager.FindByEmailAsync(user.Email);
        if (i == null) {
            var adminUser = await _userManager.CreateAsync(user, "Something*");
            if (adminUser.Succeeded) {
                await _userManager.AddToRoleAsync(user, adminRole);
                //
                _logger.LogInformation("Create {0}", user.UserName);
            }
        }
    }
    //! By: Luis Harvey Triana Vega
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*man 6

以下代码将适用于 ISA。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, 
        IServiceProvider serviceProvider)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

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

        CreateRolesAndAdminUser(serviceProvider);
    }

    private static void CreateRolesAndAdminUser(IServiceProvider serviceProvider)
    {
        const string adminRoleName = "Administrator";
        string[] roleNames = { adminRoleName, "Manager", "Member" };

        foreach (string roleName in roleNames)
        {
            CreateRole(serviceProvider, roleName);
        }

        // Get these value from "appsettings.json" file.
        string adminUserEmail = "someone22@somewhere.com";
        string adminPwd = "_AStrongP1@ssword!";
        AddUserToRole(serviceProvider, adminUserEmail, adminPwd, adminRoleName);
    }

    /// <summary>
    /// Create a role if not exists.
    /// </summary>
    /// <param name="serviceProvider">Service Provider</param>
    /// <param name="roleName">Role Name</param>
    private static void CreateRole(IServiceProvider serviceProvider, string roleName)
    {
        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

        Task<bool> roleExists = roleManager.RoleExistsAsync(roleName);
        roleExists.Wait();

        if (!roleExists.Result)
        {
            Task<IdentityResult> roleResult = roleManager.CreateAsync(new IdentityRole(roleName));
            roleResult.Wait();
        }
    }

    /// <summary>
    /// Add user to a role if the user exists, otherwise, create the user and adds him to the role.
    /// </summary>
    /// <param name="serviceProvider">Service Provider</param>
    /// <param name="userEmail">User Email</param>
    /// <param name="userPwd">User Password. Used to create the user if not exists.</param>
    /// <param name="roleName">Role Name</param>
    private static void AddUserToRole(IServiceProvider serviceProvider, string userEmail, 
        string userPwd, string roleName)
    {
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

        Task<ApplicationUser> checkAppUser = userManager.FindByEmailAsync(userEmail);
        checkAppUser.Wait();

        ApplicationUser appUser = checkAppUser.Result;

        if (checkAppUser.Result == null)
        {
            ApplicationUser newAppUser = new ApplicationUser
            {
                Email = userEmail,
                UserName = userEmail
            };

            Task<IdentityResult> taskCreateAppUser = userManager.CreateAsync(newAppUser, userPwd);
            taskCreateAppUser.Wait();

            if (taskCreateAppUser.Result.Succeeded)
            {
                appUser = newAppUser;
            }
        }

        Task<IdentityResult> newUserRole = userManager.AddToRoleAsync(appUser, roleName);
        newUserRole.Wait();
    }
Run Code Online (Sandbox Code Playgroud)


小智 6

在配置方法中声明您的角色管理器(启动)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager<IdentityRole> roleManager)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

        Task.Run(()=>this.CreateRoles(roleManager)).Wait();
    }


    private async Task CreateRoles(RoleManager<IdentityRole> roleManager)
    {
        foreach (string rol in this.Configuration.GetSection("Roles").Get<List<string>>())
        {
            if (!await roleManager.RoleExistsAsync(rol))
            {
                await roleManager.CreateAsync(new IdentityRole(rol));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

可选 - 在 appsettings.JSON 中(这取决于您想要从哪里获取角色)

{
"Roles": [
"SuperAdmin",
"Admin",
"Employee",
"Customer"
  ]
}
Run Code Online (Sandbox Code Playgroud)


小智 5

除了 Temi Lajumoke 的回答之外,值得注意的是,在 ASP.NET Core 2.1 MVC Web Application 中创建所需的角色并将其分配给特定用户后,启动应用程序后,您可能会遇到方法错误,例如注册或管理帐户:

\n\n
\n

InvalidOperationException: 尝试激活\n \'WebApplication.Areas.Identity.Pages.Account.Manage.IndexModel\' 时,无法解析类型\n \'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender\' 的服务。

\n
\n\n

通过添加 AddDefaultUI() 方法,可以在ConfigureServices 方法中快速纠正类似的错误:

\n\n
services.AddIdentity<IdentityUser, IdentityRole>()\n//services.AddDefaultIdentity<IdentityUser>()\n    .AddEntityFrameworkStores<ApplicationDbContext>()\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0.AddDefaultUI()\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0.AddDefaultTokenProviders();\n
Run Code Online (Sandbox Code Playgroud)\n\n

查看

\n\n

\xc2\xa0 https://blogs.msdn.microsoft.com/webdev/2018/03/02/aspnetcore-2-1-identity-ui/

\n\n

github上与\xc2\xa0相关的主题:

\n\n

\xc2\xa0 https://github.com/aspnet/Docs/issues/6784了解更多信息。

\n\n

为了将角色分配给特定用户,可以使用 IdentityUser 类而不是 ApplicationUser 类。

\n