ASP.NET标识添加角色

时间:2017-12-11 22:19:57

标签: c# asp.net-mvc web asp.net-identity

我正在使用MVC开展项目。我想使用IDENTITY Library作为会员资格。这是解决方案视图:

我创建了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);
        }
    }

当我尝试添加新成员时,它说&#34;角色管理员不存在。&#34;但是在User Table中添加了新成员,AspNet Role Table为空。之后,我搜索并在此站点中找到解决方案,我将这些行添加到我的代码中,它运行良好。

         const string roleName = "Admin";

        var role = roleManager.FindByName(roleName);
        if (role == null)
        {
            role = new Rol() { Name="Admin", Aciklama="Site Yöneticisi"};
            roleManager.Create(role);
        }

但它不合逻辑,每个注册页面都检查角色并尝试添加角色,我的问题是我应该在哪里写这个Roll添加代码,哪个层是哪个页面,因为我只需要两个或三个角色。我必须创造它们一次,我知道,但我不知道我应该做什么。谢谢你的回答抱歉我的英文不好:)

1 个答案:

答案 0 :(得分:1)

可能有帮助的是拥有一个在启动时运行的种子数据库类。 假设您使用代码优先数据库是否安全?我将展示两者的例子。

首先要在&#34; Data&#34; 文件夹中创建一个名为&#34; SeedData&#34;

的类
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);

            }

        }

    }
}

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

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

    }

然后,一旦你有了,你想要确保在你的应用程序启动时在数据库中创建的任何内容都会进入你的SeedData类。

相关问题