+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / Create Users, assign roles with datareader?

Create Users, assign roles with datareader?

2 posts, 0 answered
  1. Brent Holliman
    Brent Holliman avatar
    1 posts
    Registered:
    14 May 2004
    26 Oct 2011
    Link to this post
    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();

  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    06 Dec 2016
    28 Oct 2011
    Link to this post
    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 >>

2 posts, 0 answered