Automatic creation of membership users and saving the result to csv file

While working with Membership and roles providers, you may need to create the users based on your data and give them a random password.For example, you may have a table with customers and you want to give each customer a user/password to login to your system.

You can manually create a user for each of your customers, but sometimes you will need to automate the process especially when :

  1. You don’t want to enable public user registration.
  2. You have a large set of customers ( or whatever type of stakeholders you have), and you don't want to create a user to each of them manually.

I just did an example which create a user for each customer (based on the customer id ), and then save all the generated users and their passwords in a csv file.

You may ask “why i need the csv file, i can go to the database and get the generated users”.The problem here is that by default the membership will store the passwords in hashed format and so you will not be able to know the actual password that was assigned to the user when calling Membership.GeneratePassword method.Of course,you can change the membership settings to store the passwords in clear format, this way you can go directly to the membership tables in your database.

The code that will create the users is simple:

 public void CreateUsers()
    {
        //FileStream to the CSV file that will holds the genrated users
        //in this example, the csv file will be placed in app_data folder, but you may want to change that location...
        System.IO.FileStream fs = System.IO.File.Open(Server.MapPath("~/App_Data/users.csv"), System.IO.FileMode.OpenOrCreate);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, Encoding.UTF8);
        // write the header of the csv file
        sw.WriteLine("Customer name,UserName,Password");
        List<Customer> customers = Customer.GetAllCustomers();
 
        foreach (Customer c in customers)
        {
 
            string password = Membership.GeneratePassword(10, 3);
            MembershipCreateStatus status = new MembershipCreateStatus();
            string customerUserName = GetCustomerUserName(c);
            
            // you may need to generate the security questiomn/answer based on the customers data ?
            //or maybe disable the question/answer through mebership section in web.config and just pass null for both question and answer.
            MembershipUser muser = Membership.CreateUser(customerUserName, password, c.Email, "security question here", "security asnwer", true, out status);
            if (status == MembershipCreateStatus.Success)
            {
                // add the user to customers role.
                Roles.AddUserToRole(muser.UserName, "customer");
                // write the created user to csv file
                sw.WriteLine(c.CustomerName + "," + muser.UserName + "," + password);
            }
            else
                // write to output window to dispaly the failed user
                Debug.WriteLine("user creation failed for customer" + c.CustomerName + " reason: " + status.ToString());
 
        }
        sw.Close();
        fs.Close();
    }
 
    private string GetCustomerUserName(Customer c)
    {
        // just consider the customerId as the username , you can use customername or whatever you want
        return c.CustomerId;
    }

 

The customer class:

public class Customer
{
 
    public string CustomerId
    {
        get;
        set;
    }
 
    public string CustomerName
    {
        get;
        set;
    }
 
    public string Address
    {
        get;
        set;
    }
    public string Email
    {
        get;
        set;
    }
    public static List<Customer> GetAllCustomers()
    {
        // for the sake of demonstration, we just return a dummy list of customers 
        return new List<Customer>() {
            new Customer() { CustomerId = "c1", CustomerName = "cusomter 1", Address = "address 1",Email="Email@customer1company.com" },
            new Customer() { CustomerId = "c2", CustomerName = "cusomter 2", Address = "address 2",Email="Email@customer2company.com" },
            new Customer() { CustomerId = "c3", CustomerName = "cusomter 3", Address = "address 3" ,Email="Email@customer2company.com" },
        };
    }
}

 

Anas Ghanem

2 Comments

Comments have been disabled for this content.