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.
Related
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)
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!!
I'm setting up membership in my ASP.NET MVC4 app, so I open the solution and then from there, I open up the ASP.NET Configuration website option under the "Project" menu. This opens up the ASP.NET Configuration tool to create/modify/delete users. What I'm finding is when I create a user, it creates the user in a local database when I specifically set in the web.config to point to a DIFFERENT database hosted with my web hosting company. I put <remove name="LocalSqlServer" /> to remove the connection defined in machine.config.
Here's the connection string in the web.config for my app:
<remove name="LocalSqlServer" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=cp.speedwell.arvixe.com; Initial Catalog=*********; User ID=********; Password=********;" />
Here's my database structure that's with my web hosting company. As you can see, I have all of the aspnet_ tables already created:
When I click on "Providers" in the ASP.NET Configuration tab, here's my list of providers:
DefaultMembershipProvider is pointed to "DefaultConnection" which is the connection string to the database hosted with my web hosting company. When I click "Test", it immediately comes back saying:
Finally, when I click on Security, here's the message I get (keep in mind I removed LocalSqlServer from my connection strings):
Any ideas what I'm doing wrong?
UPDATE
Here are my Membership, Profile, RoleManager, and SessionState sections of my 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="DefaultMembershipProvider">
<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 enabled="true" defaultProvider="DefaultRoleProvider">
<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>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
In your Web.config,
Just replace this,
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
with this,
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
You do not need to remove LocalSqlServer instead you need to remove DefaultConnection.
<remove name="DefaultConnection" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" .../>
Beside, if you use new ASP.Net Universal Providers, you do not need to use aspnet_regsql.exe to generate tables which is deprecated.
ASP.Net Universal Providers uses Entity Framework.
If your database is new (no existing data), my suggestion will be to delete that database and use ASP.Net Universal Providers to create tables (no need to use aspnet_regsql.exe).
Place <clear/> inside tag before add.
<profile defaultProvider="DefaultProfileProvider">
<providers>
<clear/>
<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="DefaultMembershipProvider">
<providers>
<clear/>
<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 enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<clear/>
<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>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<clear/>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
Is your web.config pointing to that connection string for membership via connectionStringName like so:
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="NameOfConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
Or could you be having build configuration issues? Have you verified that the uploaded config file is pointing to the right place?
Also try adding a clear before your declaration of membership:
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<clear />
<add name="DefaultMembershipProvider" ...
Turns out, my Users/Roles/Etc were actually being inserted into the correct database all along- they were being inserted into the Users table, not the aspnet_Users table, as I would expect. This is because my app was configured to use the asp.net universal providers.
Since I'm using MYWSAT, and it requires the old aspnet tables, I removed the universal providers and it now has moved past that problem. The reason I was getting that message saying that it couldn't establish a connection to the database was exactly because of this.
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>
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>