简体   繁体   中英

Asp.net c# Adding user roles

In my application I am trying to add Roles and then add Users to a particular role. After searching online I have made this snippet

public ActionResult Install1()
{
    ClearLocalDev();

    RegisterBindingModel model = new RegisterBindingModel();
    model.Email = "mohsin@crondale.com";
    model.Password = "123Asd?";
    model.ConfirmPassword = "123Asd?";
    CreateUser(model);
    return RedirectToAction("Index", "Home");
}

public void CreateUser(RegisterBindingModel model)
{

    ApplicationDbContext context = new ApplicationDbContext();
    IdentityResult identityRoleResult;
    IdentityResult identityUserResult;

    var roleStore = new RoleStore<IdentityRole>(context); // The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

    var roleMgr = new RoleManager<IdentityRole>(roleStore);

    if (!roleMgr.RoleExists("Admin"))
    {
        identityRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
    }

    var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    var appUser = new ApplicationUser
    {
        UserName = model.Email,
        Email = model.Email
    };
    identityUserResult = userMgr.Create(appUser, model.Password);

    if (!userMgr.IsInRole(userMgr.FindByEmail(model.Email).Id, "Admin"))
    {
        identityUserResult = userMgr.AddToRole(userMgr.FindByEmail(model.Email).Id, "Admin");
    }

}

This doesn't get. I get an exception. See the comments in the code to see what and where do I get the error.

Does it have something to do with Async?

I am using Azure as my data storage.

This should work for 4.5:

ApplicationDbContext context = new ApplicationDbContext();
IdentityResult identityRoleResult;
IdentityResult identityUserResult;

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

if (!roleMgr.RoleExists("SuperAdmin"))
{
    identityRoleResult = roleMgr.Create(new IdentityRole { Name = "SuperAdmin" });
}

var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
    UserName = "SuperAdminUser@wingtiptoys.com",
    Email = "SuperAdminUser@wingtiptoys.com"
};
identityUserResult = userMgr.Create(appUser, "Pa$$word1");

if (!userMgr.IsInRole(userMgr.FindByEmail("SuperAdminUser@xyz.com").Id, "SuperAdmin"))
{
    identityUserResult = userMgr.AddToRole(userMgr.FindByEmail("SuperAdminUser@wingtiptoys.com").Id, "SuperAdmin");
}

Have you created the appropriate database tables for identity provider by running ef migrations? To me it seems that you are trying to invoke a context while the underlying database tables have not yet been created.

I suspect the following line will also cause problems:

identityUserResult = userMgr.Create(appUser, model.Password);

Two things you need to check:

  1. You must hash your password before attempting to save it
  2. The password must adhere to the configured password policy before attempting to save it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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