Add Role to User after they Register RRS feed

  • Question

  • User1429525508 posted

    I am trying to add a role to a user when they register in my MVC 5 project. I tried adding in RoleStore, RoleManager, UserStore, and UserManager as seen in the code below, but when I register I get an error stating "Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion'.

    I changed my connection string in the IdentityModels.cs ---> ApplicationDbContext( ) so that when I register a new user the data is stored in SQL Server, which works fine if I exclude the AddToRole section of this code. 

    How can I add a role to a user when they register?

    // POST: /Account/Register
    public async Task<ActionResult> Register(RegisterViewModel model)
    	using (var context = new ApplicationDbContext())
    		if (ModelState.IsValid)
    			var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
    			var result = await UserManager.CreateAsync(user, model.Password);
    			var roleStore = new RoleStore<IdentityRole>(context);
    			var roleManager = new RoleManager<IdentityRole>(roleStore);
    			var userStore = new UserStore<ApplicationUser>(context);
    			var userManager = new UserManager<ApplicationUser>(userStore);
    			if (!Roles.RoleExists("User"))
    			userManager.AddToRole(user.Id, "User");
    			if (result.Succeeded)
    				await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
    				return RedirectToAction("Index", "Home");
    		// If we got this far, something failed, redisplay form
    		return View(model);

    Tuesday, October 21, 2014 8:44 PM

All replies

  • User-1716253493 posted

    seem like the membership database is created using deferent net version.

    use req_sql in appropriate net version folder when registering sql membership provider

    Tuesday, October 21, 2014 9:08 PM
  • User1429525508 posted

    Thank you for your help. I was trying to find examples of using req_sql in appropriate net version folder when registering sql membership provider but I was unable to come up with a solution. Could you please provide an example of how this would be done.

    Thank you,


    Thursday, October 23, 2014 11:32 AM