I don't really understand how to work with this Identity thing, below I tried my best, I don't have errors but its doing nothing and I wonder why. I also checked this:
Adding Role dynamically in new VS 2013 Identity UserManager
Add User to Role ASP.NET Identity
Table in database is modified I have: Id (auto-increment, Primary Key), RoleId, UserId - both by default and without Primary Key.
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var roleStore = new RoleStore<IdentityRole>();
var manager = new UserManager<IdentityUser>(userStore);
var RoleManager = new RoleManager<IdentityRole>(roleStore);
var user = new IdentityUser { UserName = CreateUserWizard1.UserName };
var role = new IdentityRole { Id = RadioButtonList1.SelectedValue};
//Create new user and try to store in DB.
if (CreateUserWizard1.Password.ToString() == CreateUserWizard1.ConfirmPassword.ToString())
{
try
{
IdentityResult result = manager.Create(user, CreateUserWizard1.Password.ToString());
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
var selectedRole = RadioButtonList1.SelectedValue;
var currentUser = manager.FindByName(user.UserName);
var roleResult = manager.AddToRole(currentUser.Id, selectedRole);
authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);
.
.
I am not looking for classic solution made with visual studio. I have user profiles with SQL and are working fine, but my roles are now messed up. They are working fine if I put manually in that SQL table RoleId and UserId thought.
Did you try using the other UserStore Constructor and the other RoleStore Constructor.
They are both accepting the database context as parameters.
In both links you provided the constructor with parameter is used.