It seems like it is necessary to add a line in Appsettings of web.config to enable SimpleMembership. Is this correct? Also, where do I place the following line to use my own db for storing the user info. In what file should it go?
System.Reflection.TargetInvocationException was unhandled by user code HResult=-2146232828 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at System.Threading.LazyHelpers`1.ActivatorFactorySelector() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock) at TravelPaths.InitializeSimpleMembershipAttribute.OnActionExecuting(ActionExecutingContext filterContext) in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 16 at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<>c__DisplayClass3b.<BeginInvokeActionMethodWithFilters>b__35() at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) InnerException: System.InvalidOperationException HResult=-2146233079 Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588 Source=TravelPaths StackTrace: at TravelPaths.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 33 InnerException: System.InvalidOperationException HResult=-2146233079 Message=The "WebSecurity.InitializeDatabaseConnection" method can be called only once. Source=WebMatrix.WebData StackTrace: at WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) at WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) at WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) at TravelPaths.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 31 InnerException:
InnerException: System.InvalidOperationException HResult=-2146233079 Message=The "WebSecurity.InitializeDatabaseConnection" method can be called only once. Source=WebMatrix.WebData
Look at the exception message, it is already initialized, you can remove either initialization code to make it work.
I have found that the way to make this work is to add the connection string to your users database in web.config, ConnectionStrings section, for example:
the arguments should correspond to your connection string name, your table with users, your pk column
and columname for storing the users.
Make sure not to put the above line in _Appstart.vbhtml (or .cshtml), that is what was causing the double
call to initialize.
MS should provide a better way to accomplish this without having to modify InitializeSimpleMembershipAttribute.
Perhaps allowing the values for the table name, pk column, and user name column to be specified in AppSettings.
MS should provide a better way to accomplish this without having to modify InitializeSimpleMembershipAttribute. Perhaps allowing the values for the table name, pk column, and user name column to be specified in AppSettings.
You can always specify them in the config file and pull the value in run time using ConfigurationManager.
It results that on MVC 4 Internet template that's not needed anymore, just have to go to Filters/InitializeSimpleMembershipAttribute.cs and modify the same line under the public method SimpleMemberShipInitializer, as shown below:
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("DefaultConnection", "tblUsuario", "idUsuario", "UserName", 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);
}
}
}
}
After that SimpleMembership worked like a charm!
BTW, I'm using Azure SQL Database as storage for membership information, not problems at all.
LuisCarlos
0 Points
3 Posts
How to enable SimpleMembership in MVC 4
Sep 03, 2012 08:59 PM|LINK
It seems like it is necessary to add a line in Appsettings of web.config to enable SimpleMembership. Is this correct? Also, where do I place the following line to use my own db for storing the user info. In what file should it go?
WebSecurity.InitializeDatabaseFile("MySecurity.sdf", "Users", "UserID", "Username", true);
Consultant
CPrakash82
All-Star
18720 Points
2899 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 03, 2012 09:05 PM|LINK
In Web.config - AppSettings-
<add key="enableSimpleMembership" value="true" />
Write the below line in _AppStart.cshtml
WebSecurity.InitializeDatabaseFile("MySecurity.sdf", "Users", "UserID", "Username", true);Look here for more information.
LuisCarlos
0 Points
3 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 04, 2012 05:59 PM|LINK
I have tried the settings indicated but still does not work. First the correct line to include in _Appstart is:
WebSecurity.InitializeDatabaseConnection("MyConnection", "Users", "UserID", "Username", true)where MyConnection is the name of a connection string to db with user membership data.I get an exception inside InitializeSimpleMembershipAttribute.vb in the line following code:LazyInitializer.EnsureInitialized(_initializer, _isInitialized, _initializerLock)
System.Reflection.TargetInvocationException was unhandled by user code HResult=-2146232828 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at System.Threading.LazyHelpers`1.ActivatorFactorySelector() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock) at TravelPaths.InitializeSimpleMembershipAttribute.OnActionExecuting(ActionExecutingContext filterContext) in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 16 at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<>c__DisplayClass3b.<BeginInvokeActionMethodWithFilters>b__35() at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) InnerException: System.InvalidOperationException HResult=-2146233079 Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588 Source=TravelPaths StackTrace: at TravelPaths.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 33 InnerException: System.InvalidOperationException HResult=-2146233079 Message=The "WebSecurity.InitializeDatabaseConnection" method can be called only once. Source=WebMatrix.WebData StackTrace: at WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) at WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) at WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) at TravelPaths.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in C:\Users\Luis\Documents\Visual Studio 2012\TravelPaths\TravelPaths\Filters\InitializeSimpleMembershipAttribute.vb:line 31 InnerException:
Consultant
CPrakash82
All-Star
18720 Points
2899 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 04, 2012 11:58 PM|LINK
Look at the exception message, it is already initialized, you can remove either initialization code to make it work.
teguhyuliant...
Participant
1370 Points
372 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 05, 2012 02:12 AM|LINK
This link below will explain to you how to enable simple membership in MVC 4 :
http://vantsuyoshi.wordpress.com/2012/02/24/create-simplemembership-with-asp-net-mvc-4-beta-sql-compact-edition-4ef-4-3-code-first/
tayebi
Member
65 Points
13 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 05, 2012 06:06 AM|LINK
Please have look at
http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx
LuisCarlos
0 Points
3 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 05, 2012 10:11 AM|LINK
I have found that the way to make this work is to add the connection string to your users database in web.config, ConnectionStrings section, for example:
<add name="MyTravelsConn" connectionString="datasource=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\MyTravels.mdf;integrated security=True;connect timeout=30" providerName="System.Data.SqlClient" />
In the AppSettings include a line:
<add key="enableSimpleMembership" value="true" />
Then in folder Filters modify InitializeSimpleMembershipAttribute. The line InitializeDatabaseConnection should be:
WebSecurity.InitializeDatabaseConnection("MyTravelsConn", "Users", "pkUserID", "Username", True)
the arguments should correspond to your connection string name, your table with users, your pk column and columname for storing the users.
Make sure not to put the above line in _Appstart.vbhtml (or .cshtml), that is what was causing the double call to initialize.
MS should provide a better way to accomplish this without having to modify InitializeSimpleMembershipAttribute. Perhaps allowing the values for the table name, pk column, and user name column to be specified in AppSettings.
Consultant
CPrakash82
All-Star
18720 Points
2899 Posts
Re: How to enable SimpleMembership in MVC 4
Sep 05, 2012 11:01 AM|LINK
Glad that you got it working.
You can always specify them in the config file and pull the value in run time using ConfigurationManager.
dhabed
Member
2 Points
1 Post
Re: How to enable SimpleMembership in MVC 4
Dec 21, 2012 02:50 PM|LINK
I had the same problem and I was trying to add the line:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "tblUser", "idUser", "UserName", true);at the file _AppStart.cshtml
It results that on MVC 4 Internet template that's not needed anymore, just have to go to Filters/InitializeSimpleMembershipAttribute.cs and modify the same line under the public method SimpleMemberShipInitializer, as shown below:
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("DefaultConnection", "tblUsuario", "idUsuario", "UserName", 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); } } } }After that SimpleMembership worked like a charm!
BTW, I'm using Azure SQL Database as storage for membership information, not problems at all.
Cheers.
Dánfer.