C# 如何在ASP.Net标识中创建角色和用户?

C# 如何在ASP.Net标识中创建角色和用户?,c#,asp.net,asp.net-identity,asp.net-roles,C#,Asp.net,Asp.net Identity,Asp.net Roles,当我的web应用程序启动时(在Global.asax.cs中的应用程序_Start()中),我使用以下方法创建了两个角色和两个用户。 但是,正在创建管理员角色,而不是用户角色。对于名为Admin@admin.com和用户名user@user.net. 正在创建第一个,但未创建第二个 这是我的密码 void create() { ApplicationDbContext context = new ApplicationDbContext(); IdentityResult IdR

当我的web应用程序启动时(在Global.asax.cs中的应用程序_Start()中),我使用以下方法创建了两个角色和两个用户。 但是,正在创建管理员角色,而不是用户角色。对于名为Admin@admin.com和用户名user@user.net. 正在创建第一个,但未创建第二个

这是我的密码

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");
}
void create(){
ApplicationDbContext上下文=新的ApplicationDbContext();
识别结果IdRoleResult;
IdentityResult IdUserResult;
var roleStore=新roleStore(上下文);
var roleMngr=新角色管理器(角色存储);
如果(!roleMngr.RoleExists(“管理员”))
IdRoleResult=roleMngr.Create(新标识(“管理员”);
roleStore=新roleStore(上下文);
roleMngr=新的RoleManager(roleStore);
如果(!roleMngr.RoleExists(“用户”))
IdRoleResult=roleMngr.Create(新的IdentityRole(“用户”));
var userMngr=newusermanager(newuserstore(context));
var appUser=new ApplicationUser(){UserName=”Admin@admin.com" };
IdUserResult=userMngr.Create(appUser,“pa$$word”);
if(IdUserResult.successed)
idrolesult=userMngr.AddToRole(appUser.Id,“管理员”);
userMngr=newusermanager(newuserstore(context));
appUser=new ApplicationUser(){UserName=”user@user.net" };
IdUserResult=userMngr.Create(appUser,“user”);
if(IdUserResult.successed)
idrolesult=userMngr.AddToRole(appUser.Id,“用户”);
}
有谁能告诉我,我做错了什么,或者有没有其他的方法来做这件事。 提前谢谢

更新代码:

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.");
    }
}
void createAdmin(){
ApplicationDbContext上下文=新的ApplicationDbContext();
识别结果IdRoleResult;
IdentityResult IdUserResult;
var roleStore=新roleStore(上下文);
var roleMngr=新角色管理器(角色存储);
如果(!roleMngr.RoleExists(“管理员”)){
IdRoleResult=roleMngr.Create(新标识(“管理员”);
如果(!idrolesult.successed)
抛出新异常(“未创建管理员角色”);
}
如果(!roleMngr.RoleExists(“用户”)){
IdRoleResult=roleMngr.Create(新的IdentityRole(“用户”));
如果(!idrolesult.successed)
抛出新异常(“未创建用户角色”);
}
var userMngr=newusermanager(newuserstore(context));
应用程序用户appUser;
var q=来自userMngr.Users中的用户
其中user.UserName==”Admin@admin.com"
选择用户;
如果(q.Count()==0){
appUser=new ApplicationUser(){UserName=”Admin@admin.com" };
IdUserResult=userMngr.Create(appUser,“pa$$word”);
if(IdUserResult.successed){
idrolesult=userMngr.AddToRole(appUser.Id,“管理员”);
如果(!idrolesult.successed)
抛出新异常(“管理员用户未添加到管理员角色。”);
}否则
抛出新异常(“未创建管理员用户”);
}
q=来自userMngr.Users中的用户
其中user.UserName==”user@user.net"
选择用户;
如果(q.Count()==0){
appUser=new ApplicationUser(){UserName=”user@user.net" };
IdUserResult=userMngr.Create(appUser,“user”);
if(IdUserResult.successed){
idrolesult=userMngr.AddToRole(appUser.Id,“用户”);
如果(!idrolesult.successed)
抛出新异常(“用户未添加到用户角色。”);
}否则
抛出新异常(“未创建用户”);
}
}
在这里,我发现代码抛出异常,并显示消息“用户未创建”

我认为您应该读取对象结果中的错误
'IdUserResult'
,并使用函数
CreateAsync()
插入用户


我认为您应该读取对象结果中的错误
'IdUserResult'
,并使用函数
CreateAsync()
插入用户

那么,如果无法创建
用户
角色,
idrolesult
的值是多少?您需要查看这些返回值以确定发生了什么。为什么要为每个操作创建新的
RoleStore
rolemager
实例?这些可以重复使用。与
UserManager
相同。如果(IdUserResult.successed)idrolesult=userMngr.AddToRole(appUser.Id,“Administrator”)。相反,我更喜欢实现一个
switch
语句,默认情况下抛出一个
ArgumentOutOfRangeException
。这样,事情就不会悄悄地失败。您不需要每次都创建
UserManager
rolemager
RoleStore
UserStore
的新实例。您可以在开始时创建每个实例并重用它们。
IdentityResult
类具有。您需要查看该集合,例如,
idrolesult.Errors
,并找出这些错误是什么。那么,在无法创建
用户
角色的情况下,
idrolesult
的值是什么?您需要查看这些返回值以确定发生了什么。为什么要为每个操作创建新的
RoleStore
rolemager
实例?这些可以重复使用。与
UserManager
相同。如果(IdUserResult.successed)idrolesult=userMngr.AddToRole(appUser.Id,“Administrator”)。相反,我更喜欢实现一个
switch
语句,默认情况下抛出一个
ArgumentOutOfRangeException
。这样,事情就不会悄悄地失败。您不需要每次都创建
UserManager
rolemager
RoleStore
UserStore
的新实例。您可以在开始时创建每个实例并重用它们。
IdentityResult
类具有。你需要我
throw new Exception("User user wasn't created.");