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

时间:2016-07-07 16:00:41

标签: c# asp.net asp.net-identity asp.net-roles

我使用以下方法在我的Web应用程序启动时创建两个角色和两个用户(在Global.asax.cs中的Application_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 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.");
    }
}

在这里,我发现,代码抛出了异常,并显示消息“未创建用户用户。”

1 个答案:

答案 0 :(得分:0)

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

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