Create Users, assign roles with datareader?

Posted by Community Admin on 05-Aug-2018 01:47

Create Users, assign roles with datareader?

All Replies

Posted by Community Admin on 26-Oct-2011 00:00

The first iteration works fine and the new user is created. The second time through the var user is set to null.  Any thoughts? Thanks in advance.

while (reader.Read())
            
                var userManager = UserManager.GetManager("Default");
                System.Web.Security.MembershipCreateStatus status;
                userManager.Provider.SuppressSecurityChecks = true;
                var eeid = reader["eeid"];
 
                var user = userManager.CreateUser(reader["eeid"].ToString(), reader["dob"].ToString(), "email@test.com", "Question1", "Answer1", true, null, out status);
                user.FirstName = reader["fname"].ToString();
                user.LastName = reader["lname"].ToString();
 
                user.IsBackendUser = false;
 
                userManager.SaveChanges();
 
                RoleManager roleManager = RoleManager.GetManager();
 
                roleManager.Provider.SuppressSecurityChecks = true;
 
                var role = roleManager.GetRole("Union");
 
                roleManager.AddUserToRole(user, role);
 
                roleManager.SaveChanges();
 
 
            
 
            reader.Close();
            cmd.Connection.Close();

Posted by Community Admin on 28-Oct-2011 00:00

Hello Brent,

If you run the API the user will be created, but then you will have to change username, e-mail to execute it again since there is already such user.

Regards,
Stanislav Velikov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

This thread is closed