locked
Question about adding user roles. RRS feed

  • Question

  • User1451609391 posted

    I am trying to add user roles to my currently created users. The following is how far i have come so far.

    I put this in my startup.auth.cs

    public class ApplicationRoleManager : RoleManager<IdentityRole>
            {
                public ApplicationRoleManager(IRoleStore<IdentityRole, string> store) : base(store)
                {
                }
    
                public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
                {
                    var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
                    return new ApplicationRoleManager(roleStore);
                }
            }

    Then this is the same file

    app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    

    Then I added this to my accountcontroller in the login success code section

                        if (model.Email.Equals("@gmail.com") || model.Email.Equals("@GMAIL.COM"))
                        {
    
                            var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
    
    
                            if (!roleManager.RoleExists("Admin"))
                            {
                                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                                role.Name = "Admin";
                                roleManager.Create(role);
    
                            }
    
                            ApplicationUser user2 = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
    
                            //     if (user2 != null)
                            //   {
                            userManager.AddToRole(user2.Id, "Admin");
                          //  }
                             
                        }
    

    Now i get null reference exception on this line userManager.AddToRole(user2.Id, "Admin"); I heard that after the login the user is still null until the page redirects.

    Where is the best place to put this code, because my email address has already been created so i want to add the role when a user logs in. Thanks for any advice.

    Sunday, May 3, 2020 2:11 AM

All replies

  • User475983607 posted

    Now i get null reference exception on this line userManager.AddToRole(user2.Id, "Admin"); I heard that after the login the user is still null until the page redirects.

    Correct you cannot get the User Id from Current.User.Identity.GetUserId() during the login or create action.  The request for the create action does not contain the authentication cookie.  The next request will have the cookie or when the user logs in.   However, it is not clear why you do not know the UserId since  you are creating the user.  The user Id is populated when the user is created.

    Sunday, May 3, 2020 10:09 AM