简体   繁体   English

如何在ASP.Net身份中创建角色和用户?

[英]How to create Role and User in ASP.Net identity?

I used following method to create two roles and two users when my web app starts (In Application_Start() in Global.asax.cs). 我的Web应用程序启动时(在Global.asax.cs中的Application_Start()中),我使用以下方法创建了两个角色和两个用户。 However the Administrator role is being created but not the User role. 但是,正在创建管理员角色,而不是用户角色。 Similar thing happens for user named Admin@admin.com and user named user@user.net. 名为Admin@admin.com的用户和名为user@user.net的用户也会发生类似的情况。 First one is being created but not the second one. 正在创建第一个,但不创建第二个。

Here is my code. 这是我的代码。

void create() {
    ApplicationDbContext context = new ApplicationDbContext();
    IdentityResult IdRoleResult;
    IdentityResult IdUserResult;

    var roleStore = new RoleStore<IdentityRole>(context);
    var roleMngr = new RoleManager<IdentityRole>(roleStore);
    if (!roleMngr.RoleExists("Administrator"))
        IdRoleResult = roleMngr.Create(new IdentityRole("Administrator"));

    roleStore = new RoleStore<IdentityRole>(context);
    roleMngr = new RoleManager<IdentityRole>(roleStore);
    if (!roleMngr.RoleExists("User"))
        IdRoleResult = roleMngr.Create(new IdentityRole("User"));

    var userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    var appUser = new ApplicationUser() { UserName = "Admin@admin.com" };
    IdUserResult = userMngr.Create(appUser, "pa$$word");
    if (IdUserResult.Succeeded) 
        IdRoleResult = userMngr.AddToRole(appUser.Id, "Administrator");

    userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    appUser = new ApplicationUser() { UserName = "user@user.net" };
    IdUserResult = userMngr.Create(appUser, "user");
    if (IdUserResult.Succeeded)
        IdRoleResult = userMngr.AddToRole(appUser.Id, "User");
}

Can anybody tell me, what I've done wrong or any alternative way to perform this. 谁能告诉我,我做错了什么或执行此操作的任何其他方法。 Thanks in advance. 提前致谢。

Updated Code: 更新的代码:

void createAdmin() {
    ApplicationDbContext context = new ApplicationDbContext();
    IdentityResult IdRoleResult;
    IdentityResult IdUserResult;

    var roleStore = new RoleStore<IdentityRole>(context);
    var roleMngr = new RoleManager<IdentityRole>(roleStore);

    if (!roleMngr.RoleExists("Administrator")) {
        IdRoleResult = roleMngr.Create(new IdentityRole("Administrator"));
        if (!IdRoleResult.Succeeded)
            throw new Exception("Administrator role wasnt created.");
    }

    if (!roleMngr.RoleExists("User")) {
        IdRoleResult = roleMngr.Create(new IdentityRole("User"));
        if (!IdRoleResult.Succeeded)
            throw new Exception("User role wasnt created.");
    }

    var userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    ApplicationUser appUser;

    var q = from user in userMngr.Users
            where user.UserName == "Admin@admin.com"
            select user;
    if (q.Count() == 0) {
        appUser = new ApplicationUser() { UserName = "Admin@admin.com" };
        IdUserResult = userMngr.Create(appUser, "pa$$word");
        if (IdUserResult.Succeeded) {
            IdRoleResult = userMngr.AddToRole(appUser.Id, "Administrator");
            if (!IdRoleResult.Succeeded)
                throw new Exception("Admin user wasn't added to Administrator role.");
        } else
            throw new Exception("Admin user wasn't created.");
    }

    q = from user in userMngr.Users
        where user.UserName == "user@user.net"
        select user;
    if (q.Count() == 0) {
        appUser = new ApplicationUser() { UserName = "user@user.net" };
        IdUserResult = userMngr.Create(appUser, "user");
        if (IdUserResult.Succeeded) {
            IdRoleResult = userMngr.AddToRole(appUser.Id, "User");
            if (!IdRoleResult.Succeeded)
                throw new Exception("User user wasn't added to User role.");
        } else
            throw new Exception("User user wasn't created.");
    }
}

Here I found that, the code is throwing exception with message "User user wasn't created." 在这里,我发现该代码引发异常,并显示消息“未创建用户”。

throw new Exception("User user wasn't created.");

我认为您应该在对象结果'IdUserResult'读取错误,并使用功能CreateAsync()插入用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM