C# ASP.NET身份添加角色

C# ASP.NET身份添加角色,c#,asp.net-mvc,web,asp.net-identity,C#,Asp.net Mvc,Web,Asp.net Identity,我正在用MVC做一个项目。我想使用身份库作为会员。这是解决方案视图: 我创建了accountController并添加了Registerview.: public async Task<ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); var userManager = MemberShipTo

我正在用MVC做一个项目。我想使用身份库作为会员。这是解决方案视图:

我创建了accountController并添加了Registerview.:

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (!ModelState.IsValid) return View(model);

        var userManager = MemberShipTools.NewUserManager();

        var roleManager = MemberShipTools.NewRoleManager();
        var checkUser = userManager.FindByName(model.TCNo);



        if (checkUser!=null)
        {
            ModelState.AddModelError(string.Empty, "Bu TC No sistemde kayıtlı");
            return View(model);
        }

        checkUser = userManager.FindByEmail(model.Email);
        if (checkUser!=null)
        {
            ModelState.AddModelError(string.Empty, "Bu mail adresi sistemde kayıtlı");
            return View(model);
        }

        var user = new Kullanici()
        {
            Ad=model.Ad,
            Soyad=model.Soyad,
            Email=model.Email,
            UserName=model.TCNo,        
        };

        var response = userManager.Create(user, model.Sifre);

        if (response.Succeeded)
        {
            if (userManager.Users.ToList().Count() == 1)
            {
                userManager.AddToRole(user.Id, "Admin");
            }
            else
            {
                userManager.AddToRole(user.Id, "Passive");
            }
            return RedirectToAction("Login", "Hesap");
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Kayıt işleminde bir hata oluiştu");
            return View(model);
        }
    }

但这不符合逻辑,每个注册页面我都会检查角色并尝试添加角色,我的问题是我应该在哪里编写这个滚动添加代码,哪一层哪一页,因为我只需要两到三个角色。我必须创造他们一次,我知道,但我不知道我应该在哪里这样做。感谢您的回答抱歉我的英语不好:)

一个可能有用的方法是在启动时运行seed数据库类。 假设您使用的是代码优先数据库,安全吗?我将举两个例子

因此,在“数据”文件夹中要做的第一件事是创建一个名为“种子数据”的类

然后,在Startup.cs类中完成后,我添加以下行:

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<ApplicationDbContext>(options =>
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddScoped<SeedData>(); <------ This Line

    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));

services.AddScoped();您需要在插入
Rol
的位置插入所有角色。这样您就不必每次都检查以插入角色。我不能使用IServiceCollection;但我制作了seeddata类,它工作得很好。非常感谢,如果对您有效,您可以将问题标记为已回答。
using Project.Models.DatabaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Project.Data
{
    public class SeedData
    {
        private ApplicationDbContext _context;

        public ApplicationContextSeedData(ApplicationDbContext context)
        {
            _context = context;
        }
        public void EnsureSeedData()
        {

            if ("Check if roles are already there")
            {
                role = new Rol() { Name="Role1", Aciklama="Site Yöneticisi"};
                roleManager.Create(role);
                role = new Rol() { Name="Role2", Aciklama="Site Yöneticisi"};
                roleManager.Create(role);
                role = new Rol() { Name="Role3", Aciklama="Site Yöneticisi"};
                roleManager.Create(role);

            }

        }

    }
}
   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<ApplicationDbContext>(options =>
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddScoped<SeedData>(); <------ This Line

    }