Local App_Data\ASPNETDB.MDF keeps being created - asp.net-mvc

Writing an MVC5 app. For some reason, a local App_Data\ASPNETDB.MDF db keeps being created, even though in my web.config the only db I specify is a totally different one on the network (and is working fine.) What could be creating this db (and recreating, if I delete it)? I searched my solution for ASPNETDB.MDF and nothing appears, so it's not in any type of config file, etc....
The connectionStrings section of my web.config....
<connectionStrings>
<remove name="ZZZ_SpringContext" />
<add name="ZZZ_SpringContext"
connectionString="Data Source=mypc\sql2012;Initial Catalog=ZZZ_Spring;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
***** UPDATE *****
I tried deleting the db again. Then in my web.config I changed the rolemanager line to false and the db is NOT recreating now. That helps me to see what is creating the db. But, how can I get around this? Here's the line in the web.config....
<roleManager enabled="false" />
I had to change it back to "true" so my app would work correctly.

In the past I had nearly same problem where ASPNETDB.mdf file still recreate even it already deleted. It turns that ASP.NET Identity instance uses machine.config file outside the project which has the following line:
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
And in the end of configuration file I found the membership element, which seems controlling Membership provider behavior:
<system.web>
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>
<roleManager>
<providers>
<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
</system.web>
Note that connectionStringName="LocalSqlServer" refers to ASPNETDB.mdf as shown in connectionStrings element before.
Then, in web.config inside the project, this line determines if RoleManager instance is enabled:
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
The lines given above tells that ASP.NET Membership provider is enabled as current user management, which defeats purpose of ASP.NET Identity.
Hence, if the ASPNETDB.mdf database is not exist & Membership provider support being enabled, the Membership provider will initialize with default configuration settings in machine.config, and automatically generates ASPNETDB.mdf and its log file using default table definitions.
To prevent re-creation of that DB, in addition to change <roleManager enabled="false">, you can use these steps in web.config:
Clear existing or predefined connection strings before defining your own.
<connectionStrings>
<clear />
<remove name=LocalSqlServer /> <== this is maybe optional
<add name="ZZZ_SpringContext"
connectionString="Data Source=mypc\sql2012;Initial Catalog=ZZZ_Spring;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Remove RoleManager provider in module definition.
<modules>
<remove name="RoleManager" />
</modules>
Comment out all membership, profile & roleManager elements if necessary to do so.
If your account model code has UsersContext class, ensure it pointed to current connection string defined on your own.
public class UsersContext : DbContext
{
public UsersContext() : base("ZZZ_SpringContext")
{
}
}
Related issue:
How do I stop using ASPNETDB.MDF in LocalDB? (WebForms version)

Related

Login mechanism for external database

I have started developing ASP.NET MVC 4 application with the internet template. I want the login mechanism to work with an external database hosted on a full SQL server. The Internet template created by Visual Studio connects to a database on a localdb with SQLExpress. To make the login mechanism work with an external database I made the following changes:
1. Created the aspnet_schema on my external database using aspnet_regsql.exe.
2. Changed the connection string in the web.config to reflect the externaldb.
<connectionStrings>
<clear />
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="Data Source=10.183.2.22\Sandbox,1432;
Initial Catalog=ForecastView;Integrated Security=False;
User ID=peak;Password=PvS20150401;
Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" />
</connectionStrings>
3.Added the provider configuration in the web.config.
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider"
type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="SqlProvider">
<providers>
<clear />
<add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider,
System.Web,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false" maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager>
<providers>
<clear />
<add connectionStringName="DefaultConnection" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider,
System.Web, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
With the above changes the login mechanism for an external db works if I use the Mobile Application template for ASP.NET MVC 4. How to make this work for the Internet template? Any help is greatly appreciated!!

'Could not load type' error calling membership method from within asp.net mvc migrations seed method

I'm using the Seed method within configuration.cs that's created when you turn on Code First Migrations in an ASP.Net MVC project.
All my usual context.ENTITYHERE.AddOrUpdate calls are working great. The problem comes when I attempt to setup some users with this membership provider:
Could not load type 'CodeFirstMembershipSharp.CodeFirstMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
If I disable the 'Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);' line in WebSecurity.cs, it appears to be ok..!
Any ideas? Any help much appreciated!
EDIT: I've now realised this isn't that helpful as it uses an entirely different membership provider (although similar). I'm still not sure how best to resolve this but left my answer up as I hope it might help.
I know this is massively old now but I found a solution installing the latest providers. This link has the required information:
http://www.hanselman.com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx
Interestingly the web.config changes that were automatically made seem to include more information than the old.
My old provider lines were:
<membership defaultProvider="CodeFirstMembershipProvider">
<providers>
<add name="CodeFirstMembershipProvider" type="CodeFirstMembershipProvider" connectionStringName="[YOURCONNECTIONNAME]" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="CodeFirstRoleProvider">
<providers>
<clear />
<add name="CodeFirstRoleProvider" type="CodeFirstRoleProvider" connectionStringName="[YOURCONNECTIONNAME]" />
</providers>
</roleManager>
The new provider code looks like this:
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="[YOURCONNECTIONNAME]" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="[YOURCONNECTIONNAME]" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="[YOURCONNECTIONNAME]" applicationName="/" />
</providers>
</roleManager>

Asp.net configuration not able to connect to my .mdf file in APP_Data

I am running into two main problems, which I believe may be related to each other.
When I run the asp.net configuration to set up the provider, it cannot connect.
While running the project, when I try to register a new user I get this error:
"The password retrieval answer provided is invalid. Please check the value and try again."
This is in an MVC4 project, default Internet Application Template. I haven't changed anything in the project other than generating the DB using the code first approach. The database was created the APP_DATA folder. I can connect to it fine in Server Explorer in VS2012, and that connection is coming from this connection string in the web.config:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|Data Directory|\NerdlyThings.Models.NerdlyContext.mdf;Integrated Security=True;Connect Timeout=90" />
<membership>
<providers>
<add connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</membership>
<roleManager>
<providers>
<add connectionStringName="DefaultConnection" applicationName="/"
name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>

Unit test membership

I'm integration testing an MVC controller that uses the membership API. I've added my app.config below. The tests attempt to run, but when they make a call to the api, they return null. I inspect the Membership object, and it doesn't contain my connection string, it contains the default one that comes from the machine.config. It also does not pick up my configured application name. It seems to me that I have an issue with my app.config to the point where the membership api is not picking up its settings.
Can anyone spot the error?
var usr = Membership.GetUser(AValidGuid); This is always null!!!!
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="membership" type="System.Web.Configuration.MembershipSection, System.Web" />
<section name="roleManager" type="System.Web.Configuration.RoleManagerSection, System.Web" />
</configSections>
<connectionStrings>
<add connectionString="server=;user id=;password=;database=" name="SqlProvider" />
</connectionStrings>
<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<remove name="AspNetSqlMembershipProvider"></remove>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="SqlProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/test" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="AspNetSqlProvider">
<providers>
<remove name="AspNetSqlRoleProvider"></remove>
<add connectionStringName="SqlProvider" applicationName="/test" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
</configuration>
Your app.config will need to be updated for the application executing the test. Is that what the posted app.config applies to? I know that's a basic thing but a lot of people get hung up on which config is being utilized by the test runner.

ASP.NET Web Site Administration Tool unkown Error ASP.NET 4 VS 2010

I was following the MVCMusic tutorial with an machine with full sql server 2008 r2
and full visual studio professional, in ASP.NET 4.0 and when I got to the page where it sets up membership (near page 66) the Web administration tool wont work, i got the following error:
An error was encountered. Please return to the previous page and try again.
my web config is like this:
<connectionStrings>
<clear />
<add name="MvcMusicStoreCN" connectionString="Data Source=.;Initial Catalog=MvcMusicStore;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MvcMusicStoreEntities" connectionString="metadata=res://*/Models.Store.csdl|res://*/Models.Store.ssdl|res://*/Models.Store.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=MvcMusicStore;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.web>
<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MvcMusicStoreCN" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10" applicationName="/" passwordFormat="Hashed" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="MvcMusicStoreCN" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear />
<add connectionStringName="MvcMusicStoreCN" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider"
type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
<customErrors mode="Off">
</customErrors>
</system.web>
EDIT: I've run the
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regsql.exe
executable and added the tables to my MvcMusicStore database sucessfully, changed the web.config from MY application (MVCMusicStore), and tryed running the ASP.NET Configuration shortcut, and got the error.
My Default Browser is firefox, and when I click the shortcut the browser doesn't even open the page, only when I right click on the tray icon and choose open in web browser.
I've solved it, I entered another project (VS 2010 doesn't let me change the default browser in an MVC project) changed the default browser, changed back to the MVC project and tryed opening the configuration again, and it worked.
Looks like the configuration doesn't allow firefox.
Thanks Anyway Raj.
Get your app to show detailed errors by turning off custom errors
http://msdn.microsoft.com/en-us/library/h0hfz6fc(VS.71).aspx
<configuration>
<system.web>
<customErrors mode="Off">
</customErrors>
</system.web>
</configuration>
I also experienced this problem and found out it was because the directory my solution was in contained a weird character. 'C:....\C#' Changing the directory to CSharp got rid of this problem.

Resources