SimpleMembership to get UserName based from UserId

I am using SimpleMembership but I get this error "You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class." I even initialize the membership at Startup but still same
error.

Product Properties

using WebMatrix.WebData;

namespace WebsiteName.POCO
{
[InitializeSimpleMembership]
public class ProductList
{
public int ProductID { get; set; }

public int CategoryID { get; set; }

public int CustomerID { get; set; }

public string CustomerUserName {
get {
SimpleMembershipProvider provider = new SimpleMembershipProvider();

//return provider.GetUserNameFromId(123);
return provider.GetUserNameFromId(CustomerID); //<<<---error here

//int ppp = WebSecurity.GetUserId(WebSecurity.CurrentUserName); //<<<---this works but I'm looking for the username based from user id
}
set {}
}
}
}

Global

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new InitializeSimpleMembershipAttribute());
}
}

imperialx

Have you try adding this to _AppStart.cshtml for your project:

@{
    WebSecurity.InitializeDatabaseConnection("DATABASENAME", "USERNAMETABLE", "USERNAMECOLUMN", "USERNAMEMAIL", true);
}

a-rad

@{ WebSecurity.InitializeDatabaseConnection("DATABASENAME", "USERNAMETABLE", "USERNAMECOLUMN", "USERNAMEMAIL", true); }

Yes I did, but same issue. :(

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace WebsiteName.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
    {
        private static SimpleMembershipInitializer _initializer;
        private static object _initializerLock = new object();
        private static bool _isInitialized;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Ensure ASP.NET Simple Membership is initialized only once per app start
            LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
        }

        private class SimpleMembershipInitializer
        {
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnectionString", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        }
    }
}

imperialx

There is a fully working example of Single Membership in the starter site.

Leave a Reply