Search code examples
asp.net-mvc-4membership-providersimplemembership

export all users and properties from .net membership provider


i need to create a handler that will list all the users who have registered on our site in csv format.

We are using the out the box functionality for Membership providers, and we have a couple of custom properties "OptIN" and "LandingPage", how would you get all the records including the custom properties.

I had a look in the SQL table and was hoping to just be able to do a sql query, but the format is not consistent between providers and profiles, depending on which part of their profiles have been completed.

are there any built in methods for getting all users with it's properties, as I'm guessing getting all users and then creating an object to get their properties would be slow on millions of records.


Solution

  • This is the answer I came up with a simple .net Handler, not sure it will scale well, but for now it works with a small selection of records

    using System;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Security;
    using System.Web.Profile;
    
    namespace Sunlight
    {
        /// <summary>
        /// Summary description for reporting
        /// </summary>
        public class Reporting : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Server.ScriptTimeout = 3000;
                context.Response.Buffer = true;
                context.Response.Clear();
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.AddHeader("content-disposition", @"attachment;filename=""SunlightExport.csv""");
                context.Response.ContentType = "text/plain";
                MembershipUserCollection users = Membership.GetAllUsers();
                context.Response.Write(
                    "Signup Type,Username,Email,EmailVefified,Name,avatarURL,Newsletter Signup,Site,Last Activity,Signup Date");
                foreach (MembershipUser user in users)
                {
                    string username = user.UserName;
                    context.Response.Write("\r\n");
                    ProfileBase profile = ProfileBase.Create(username);
                    context.Response.Write(GetAccountType(username) + ",");
                    if (!username.Contains("@"))
                    {
                        context.Response.Write("=\"" +
                                               username.RemoveCharectorsForCsv()
                                                   .Replace("twitter-", "")
                                                   .Replace("facebook-", "") + "\",");
                    }
                    else
                    {
                        context.Response.Write(username.RemoveCharectorsForCsv() + ",");
                    }
    
                    context.Response.Write(user.Email + ",");
                    context.Response.Write(user.IsApproved + ",");    
                    context.Response.Write(profile.GetPropertyValue("usersName").ToString().RemoveCharectorsForCsv() + ",");
                    context.Response.Write(profile.GetPropertyValue("avatarURL").ToString().RemoveCharectorsForCsv() + ",");
                    context.Response.Write(profile.GetPropertyValue("userSubscribe").ToString().RemoveCharectorsForCsv() + ",");
                    context.Response.Write(GetHost(profile.GetPropertyValue("originalSiteLogin").ToString().RemoveCharectorsForCsv()) + ",");
                    context.Response.Write(user.LastActivityDate + ",");
                    context.Response.Write(user.CreationDate);
                }
    
            }
    
    
            public static string GetAccountType(string username)
            {
                if (string.IsNullOrWhiteSpace(username))
                {
                    return "UNKNOWN";
                }
                username = username.ToLower();
                return
                    username.Contains("@")
                        ? "Email"
                        : username.Contains("twitter-")
                            ? "Twitter"
                            : username.Contains("facebook-") ? "Facebook" : "UNKNOWN";
            }
    
            public static string GetHost(string site)
            {
                try
                {
                    return string.IsNullOrWhiteSpace(site) ? string.Empty : new Uri(site).DnsSafeHost;
                }
                catch
                {
                    return string.Empty;
                }
    
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    
        public static class MyStringExtensions
        {
            public static string RemoveCharectorsForCsv(this string value)
            {
                return Regex.Replace(value, @"\s+|,|'", " ");
            }
        }
    
    }