1

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 = "[email protected]";
    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.

1
  • What version of the framework are you using? Commented Jan 18, 2016 at 12:03

2 Answers 2

1

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 = "[email protected]",
    Email = "[email protected]"
};
identityUserResult = userMgr.Create(appUser, "Pa$$word1");

if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "SuperAdmin"))
{
    identityUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "SuperAdmin");
}
Sign up to request clarification or add additional context in comments.

4 Comments

What about this class ApplicationDbContext(), does it have to inherit from any other class?
That is EntityFramework context. You have to store users and roles somewhere. You can inherit from IdentityDbContext<ApplicationUser> then ASP.NET will add all membership tables for you.
I am getting this error, 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.
I am not sure how I do that?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.