locked
Cannot create new Users and Roles, example that does will not login RRS feed

  • Question

  • User2142845853 posted

    Now that the login works in an asp.net WebForms c# 4.5 using the default Authentication scheme,

    am trying to make a page to add roles and assign to users.  Found an example here, https://code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97

    However when I add their code to Startup.cs the method looks to see if user role Admin exists, if not it creates it then assigns an initial user.  It does do it, you can see the Admin role created and see the new user added. 

    But you cannot login. The password/user email are perfect in the debugger, yet it will not login. the User Manager is instantiated differently in the example using the New keyword.  Tried without success to drop the code into the same namespace as Register.aspx.cs which works just fine and logs in, yet that wont work, cannot see the scope of it, wont allow the non static method.

    Looks like the example should work, it adds the new user, just refuses to accept the login credentials that are manually set.  

     private void CreateRolesandUsers()
            {
    
    
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
                var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
                var manager =  Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); //this one FAILS, does not know what "Context" is
    
    
    

    Thursday, December 13, 2018 7:07 PM

All replies

  • User475983607 posted

    The RoleManager manages the roles in the database, AspNetRoles table. adding and removing roles from the table.  The UserManager API assigns users to roles.

    Can you explain the purpose of this line since the UserManager is already defined in the line above?

    var manager =  Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); //this one FAILS, does not know what "Context" is
    

    Are you trying to detect if the roles exist in the AspNetRoles table when the application starts then add the roles if the roles do not exist?  Since, roles do not change often.  I usually write a script to add the roles to the table.  If your app requires an admin interface to add new roles then build a page to add roles.  

    Thursday, December 13, 2018 7:28 PM
  • User2142845853 posted

    Im  trying to build an interface to assign users to roles. The code from that link offers a way to create default roles in the tables, and assigns or "seeds" one user as admin?  This may be a huge waste of time.  Wasted hours trying to make this seed user work.  Can just use sql management studio and force the values.

    To answer your question, the 3rd line there is from the working method in Register.aspx.cs the method we got working yesterday, it will register users and the login/password works great.  But thinking ok why are these 2 "UserManager" casts so different?  Why is one using the NEW keyword, but the others are not? Whats the difference? Is one obsolete?

    Wondered what the problem is, because this project from the link uses the NEW keyword to create the UserManager, but the Register.aspx.cs does not, and it works.  The other one creates the user, but the password refuses to work.

    Id much rather create the roles directly from SQL script when the tables are generated. Got side tracked trying to make this project work, very bad waste of time. I just want to add roles from within the Model, derive the SQL to generate the tables, take that script to the SQL server machine.  

    Then I want to build a page that lets the ADMIN users see and modify all users, and assign them to roles, thats really what was supposed to happen

    The RoleManager manages the roles in the database, AspNetRoles table. adding and removing roles from the table.  The UserManager API assigns users to roles.

    Can you explain the purpose of this line since the UserManager is already defined in the line above?

    var manager =  Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); //this one FAILS, does not know what "Context" is

    Are you trying to detect if the roles exist in the AspNetRoles table when the application starts then add the roles if the roles do not exist?  Since, roles do not change often.  I usually write a script to add the roles to the table.  If your app requires an admin interface to add new roles then build a page to add roles.  

    Thursday, December 13, 2018 8:11 PM
  • User475983607 posted

    Im  trying to build an interface to assign users to roles. The code from that link offers a way to create default roles in the tables, and assigns or "seeds" one user as admin?  This may be a huge waste of time.  Wasted hours trying to make this seed user work.  Can just use sql management studio and force the values.

    To answer your question, the 3rd line there is from the working method in Register.aspx.cs the method we got working yesterday, it will register users and the login/password works great.  But thinking ok why are these 2 "UserManager" casts so different?  Why is one using the NEW keyword, but the others are not? Whats the difference? Is one obsolete?

    Wondered what the problem is, because this project from the link uses the NEW keyword to create the UserManager, but the Register.aspx.cs does not, and it works.  The other one creates the user, but the password refuses to work.

    Id much rather create the roles directly from SQL script when the tables are generated. Got side tracked trying to make this project work, very bad waste of time. I just want to add roles from within the Model, derive the SQL to generate the tables, take that script to the SQL server machine.  

    Then I want to build a page that lets the ADMIN users see and modify all users, and assign them to roles, thats really what was supposed to happen

    If you look at the template code you will find the UserManager is built in a Page event handler.  The original code snippet does not clarify what you are trying to do.  

    Add a Web form and the controls.  The code below is how you fire up the UserManager.  Very simple...

            protected void AddUserToRole_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }

    Thursday, December 13, 2018 10:38 PM
  • User-893317190 posted

    Hi rogersbr,

    If you use owin, you usually get the UserManager through OwinContext.

    Like this,

    HttpContext.GetOwinContext().GetUserManager<YourUserManager>()

    First you should register your userManager in your startup.cs.

    About how to use startup.cs , you could refer to , there are many ways

    https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

    In your startup.cs, you should register your userManager.

    Like your link.

     public void Configuration(IAppBuilder app)
            {
                ConfigureAuth(app);
    			createRolesandUsers();
    		}

    And this is what  the ConfigureAuth does, the classs is in the folder App_Start

     public void ConfigureAuth(IAppBuilder app)
            {
                // Configure the db context, user manager and signin manager to use a single instance per request
                app.CreatePerOwinContext(ApplicationDbContext.Create);
                app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
                app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    

    Then you could get ApplicationUserMnager through owin.

    If you want to apply role using owin , you should also register a RoleManager in ConfigureAuth method,

    like

    app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create)
    public class AppRoleManager:RoleManager<AppRole>{
    
    public AppRoleManager(RoleStore<AppRole>store):base(store)
    
    
    
    }
    public static AppRoleManager Create(IdentityFactoryOpetions<AppRoleManager>options,
    IOwinContext context ){
    return new AppRoleManager(new RoleSotre<AppRole>(context.Get<AppIdentityDbContext>)) }


    public class AppRole:IdentityRole{
    public AppRole():bas(){}
    public AppRole(string name):base(name){}
    }

    Best regards,

    Ackerly Xu

    Friday, December 14, 2018 6:34 AM