Profile command generates App-Data folder and ASPNETDB.MDF - asp.net

I am migrating a ASP.NET webforms website to a new server. On the old server I had my database in the App_Data folder in the root of the website. On my new server, I have the database in a different folder outside the root and that all works fine.
But something strange happens. When I go to the website and surf around, suddenly a App_Data folder is being created in the root folder of my site with a database in it, called ASPNETDB.MDF. I can delete it but after a while it appears again.
I did some testing and trying, and I found out that the DB is being created when I run this code:
TestLabel.Text = Profile.MyProfileParameter.ToString();
So I think it must be the Profile being called that causes it.
In my web.config I have the following: (a bit reduced, i have more profile parameters)
<profile enabled="true">
<properties>
<add name="MyProfileParameter" type="Int32" defaultValue="30"/>
</properties>
</profile>
A bit higher up in my web.config i have the connectionstring that points to the database outside my root folder:
<connectionStrings>
<add name="ConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TheNameOfMyDatase;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
Does anybody have any idea why a call to profile would generate a database?
It's not a real show stopper, but I'd like to know why this is happening.
Some extra info:
My old server has Windows Server 2003
My new server has Windows Server 2008 R2
My application is running in ASP.NET 3.5
SOLUTION:
Apparently there still a lot for me to learn about web.config.
Apart from the web.config there is also a machine.config from which the web.config inherits.
In this machine config in the section <profile> there is this setting:
<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>
This setting can be overridden in the web.config
First (I do not think this is really necessary ) I remove the localSqlServer from the connection strings by adding a clear command to my <connectionStrings> :
<connectionStrings>
<!-- clear command removes localSqlServer from machine.config -->
<clear />
<add name="ConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TheNameOfMyDatase;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
If I run my site now you get an error saying the connection name 'LocalSqlServer' was not found.
So I added a new provider to the <profile> section and point it to my ConnectionString
<profile enabled="true">
<providers>
<!-- clear command removes localSqlServer from machine.config -->
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="ConnectionString" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
Now it all works fine, no more database appearing out of the blue.
Thank you ijaz for giving directions, the bonus is yours!

AspNetdb is a database used by Asp.Net Membership provider or other application services like "profiles,Roles/Rights etc" The generated .mdf file is a database (SQL 2005 express).
It stores the information you store in your profile or membership or roles, when you use the standard sql providers in ASP.NET. App_Data is the default location of this db,but off-course it can be changed. try to find and update the section of web.config file as,
<membership>
<providers>
</providers>
</membership>for details,please have a look at the following post.
http://forums.asp.net/t/1517995.aspx

Related

ASP.Net Membership Error CREATE DATABASE failed. Some file names listed could not be created. Check related errors

I am trying to add AP.Net membership to my database. I have had success with this in the past so I have some idea of what I am doing. My project is a VB.Net website with Framework 4.5.2. I used asp_regsql.exe in framework 4 to add the schema to my db. I created the following web.config entries (per MSDN articles):
<connectionStrings>
<add name="AFKMSConnectionString" providerName="System.Data.SqlClient" connectionString="working as intended" />
<system.web>
<authentication mode="Forms" >
<forms loginUrl="Account/Login.aspx"
name=".ASPXFORMSAUTH" />
</authentication>
<!--<authorization>
<deny users="?" />
</authorization>-->
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<pages>
<namespaces>
<add namespace="System.Web.Optimization"/>
<add namespace="Microsoft.AspNet.Identity"/>
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
</controls>
</pages>
<membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add
name="SqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed" />
</providers>
</membership>
<profile enabled ="true" defaultProvider="SqlProvider">
<providers>
<clear />
<add name="SqlProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"
description="SqlProfileProvider for SampleApplication" />
</providers>
</profile>
<roleManager enabled ="true"
defaultProvider ="SqlRoleProvider" >
<providers>
<clear/>
<add name ="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"/>
</providers>
</roleManager>
My connection string works for my current functions but when I try to register a user I get the following error:
Directory lookup for the file "C:\Users\Dan\Source\Repos\AFKMS\AFKMS\App_Data\aspnet-AFKMS-140073d4-6858-4f19-9555-0edfbaadd43a.mdf" failed with the operating system error 2(The system cannot find the file specified.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
This comes from Accounts/Register,
Dim result = manager.Create(user, Password.Text)
Why is it trying to create a database? What does it look like I am missing?
<connectionStrings>
<add name="AFKMSConnectionString" connectionString="data source=.;Initial Catalog=your_db_name;integrated security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
can you provide us with code of how you create users?
you mentioned: Dim result = manager.Create(user, Password.Text)
i think you need to use:
Membership.CreateUser(UserName, Password, Email)
I'm assuming you've created a new ASP.NET Web Application, using the Web Forms templates?
In which case you appear to be attempting to configure the newer ASP.NET Identity system with the original Membership Provider configuration settings.
As Ahmed noted, you mention the call in the register page to manager.Create(user, Password.Text) - this is using the ApplicationUserManager from ASP.NET Identity.
If you look in the App_Start folder of your project, you should find a file named IdentityConfig.vb (assuming you're using VB.NET), which has a method Create that returns the ApplicationUserManager - this in turn calls the ApplicationDbContext class that is created in the Models folder, that will be defined to use a connection called "DefaultConnection" - by default this is set to be a standalone .mdf file in the App_Data folder named after the project.
You should change this to point at your connection string, but be aware that this uses Entity Framework to create the database schema for you - it should just add it to the existing database, but you should ensure you've backed it up before you start in case it drops everything first.

What is wrong with my web.config file and Role manager settings? IIS6.0 and SQL Server 2005 wont let me in

I have developed an application which works fine on my local IIS7 server. I can log into it using a different machine on the local network and access all areas.
After deploying it on an SBS2003 server with IIS6.0 and SQL Server 2005 I have had access problems. I have sorted this to the point where I can now load the web app and log in. The problem begins when I want to access a page that requires authentication. Even though I am logged in I cannot load the page. Needless to say that this applies to pages that requires RBA also.
The current error message I get is:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
If you are going to answer this question by telling me the answer to the problem is in the error message then please first remember that I can log in to my application. I obviously have a connection to the DB. After that if the answer is still simple please feel free to mock me.
Here is my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ZenIntranetConnectionString" connectionString="Data Source=127.0.0.1;Database=ZenIntranet;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="8192" executionTimeout="360"/>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ZenIntranetConnectionString"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ZenIntranetConnectionString" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="true" defaultProvider="ZenRoleProvider">
<providers>
<clear/>
<add name="ZenRoleProvider" type="BusinessClasses.ZenRoleProvider, BusinessClasses"
connectionStringName="ZenIntranetConnectionString"
applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Thanks for your help. I really really need it.
P.S I only just added the connection string name in the <roleManager> tag. I've never needed it before and my local web.config doesnt seem to require it. I've left it in their for now because I've seen that others said it was necessary. Regardless, it doesn't work.
The problem was hard coded connection strings (generated by VS) used for the dataset designer in the dataclasses layer.
I could login ok because the home page didnt load any data objects but as soon as I needed something from the dataset.xsd file the application was using the hard coded connection string from the DAL (which still had my development server connection string). I found the strings after doing a solution wide search for connectionString.
On a side note, I thought that the web.config should over ride this but it doesnt.

ASP.NET 4.5 membership does not recognize SQL Server data provider at design time?

I am new to Visual Studio 2012 / ASP.NET 4.5 web forms.
I am attempting to use forms authentication (ASP.NET membership) in my asp.net 4.5 web forms application (using Visual Studio 2012) as I have done successfully many times with asp.net version 4.0.
The problem seems to be that, when I am testing in Visual Studio 2012, it does not recognize my web.config settings pointing to an SQL Server 2008R2 aspnetdb. It seems to be using the SQL Express aspnetdb.mdf file to store my users.
I have tried removing the aspnetdb.mdf completely from my web app and it still does not recognize my web.config settings pointing to the SQL Server and instead it uses the settings from somewhere else (don't know where!). I know this because the Membership.CreateUser() method is throwing an error if I do not provide the question and answer as parameters. Also the web configuration tool requires a question and answer to create a new user. My web.config settings specifically do not!
When I host the web application, it DOES look for my users in the SQL Server database because when I try logging in with a user name that was successfully created using the web configuration tool in VS2012, my application cannot find that user! It seems to be looking in SQL Server but cannot find it.
I must be missing some fundamental setting... Anyone have any thoughts?
I have included my web.config settings below:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=localhost;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=studentid;Password=password" providerName="System.Data.SqlClient"/>
<add name="StudentAttendanceConnectionString" connectionString="Data Source=localhost;Initial Catalog=StudentAttendance;Persist Security Info=True;User ID=studentid;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
<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="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<membership>
<providers>
<add connectionStringName="ApplicationServices" enablePasswordRetrieval="true"
enablePasswordReset="false" 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="ApplicationServices" applicationName="/"
name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
A few things...
1) Do the users or some account have access to the SQL database? I don't see that noted here.
2) Use the server name of the SQL server that appears when you fire up the SQL Management Studio and not localhost if IIS and SQL are on the same server.
3) Check your machine.config file for the version of .Net you're using. I ended up having to change the SQL Express settings to the IP address of the server to get things working for me in a similar scenario along with removing (through the control panel) SQL Express or whatever test database VS 2012 installs. I had errors related to this when building a server recently.
::edit::
Also add the following to your Web.Config file above the 'connectionStrings' section:
<startup>
<supportedRuntime version="4.0" sku=".NETFramework,Version=v4.5" />
</startup>
Could you try to create role and user using ASP.Net Config?
VS2010
Or
VS 2012 > PROJECT > ASP.Net Configuration
Another one is to add clear - Don't forget to when adding providers

ASP.Net MVC 3 Configuration

i want to ask about ASP.Net Configuration. i have a project that use ASP.Net MVC3. and i use membership in my project. in my database server, there's 2 database for my project.
aspnetdb, contains aspnet_membership, aspnet_user, aspnet_roles, etc (i use asp.net Configuration)
mydatabase, its for my project database.
now my boss told me that he want to host my project in public and told me to use only one database. so i have to move aspnetdb to mydatabase. how can i do it without making new table in mydatabase ?
thanks
By default the ASP.NET membership provider uses a built-in connectionString named LocalSqlServer which is something like this:
<add name="LocalSqlServer"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
and when you use the Membership for first time (or when you use the web configuration manager tool), the aspnetdb.mdf will be created automatically. For first step, you can remove this connectionString from web.config by clear command:
<connectionStrings>
<clear/>
<!-- your other connectionStrings -->
</connectionStrings>
and also, you must change the connectionString for membershipProvider (and also roleProvider if you use it) in web.config file:
<membership userIsOnlineTimeWindow="20">
<providers>
<clear />
<add
connectionStringName="YourSpecifiedConnectionString" // your connectionString here
name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="500"
minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="YourSpecifiedConnectionString" // your connectionString here
name="AspNetSqlRoleProvider"
applicationName="/"
type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
For configure the database to use ASP.NET built-in membership provider, you can use regsql tool
which can be founded here:
C:\Windows\Microsoft.NET\Framework\(version that you are using. mine is v4.0.30319)\
In this folder, find the aspnet_regsql.exe file and run it. A helpful wizard will be shown and
help you step by step to configure new database. And about your final issue: You haven't to apply any change
to your edmx file! The EF works with tables you supplied to it, and membershipProvider do its job with
tables that ASP.NET supplied to it! Well done, Good job!
You have to add those tables to your own database. It's quite easy. Just use the aspnet_regsql tool to export the database schema to a SQL file and run that file in your own database.
Instruction:
http://www.xdevsoftware.com/blog/post/Install-ASPNET-Membership-Provider-on-a-Shared-Hosting-Company.aspx

Asp.net profile in a separate assembly

I have a web application which uses membership and profiles. I successfully used the WebProfileBuilder extension, so my profile class is correctly generated, and is working nicely.
However, after a new request of my client, I need now to move that profile management part into another assembly (so I'd be able to get profile information in a windows service running on the same machine).
What I made is created the new assembly, moved my generated profile file, and tried to use it from the other assembly, but without any success. I always get a SettingsPropertyNotFoundException. My thought is that the profile system doesn't know where to find its connection information, so I tried to add the connectionstring and provider in the app.config of this assembly, but this doesn't seem to work.
What am I missing ? Is it possible to do ?
Thanks in advance!
I've got a nasty suspicion that your APP.Config file won't be picked by the web application; did you keep the settings in your Web.Config file ?
I've only found app.config to work on such an assemply when using NUnit or similar.
Ok I found what's wrong... Thanks to this blog post:
http://fredrik.nsquared2.com/viewpost.aspx?postid=244&showfeedback=true
The only thing I needed to do is add applicationName="/" in my provider configuration, in the app.config. (which is the application name, can be found inside the aspnet_Applications table in the DB.
<configuration>
<connectionStrings>
<add
name="MyConnectionString"
connectionString="Data Source=...;Initial Catalog=...;User ID=...;Password=..."
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<profile
enabled="true"
defaultProvider="Sql2008ProfileProvider">
<properties>
<add name="UserLevel" type="integer"/>
<add name="value1" type="string" />
<add name="value2" type="string" />
<add name="value3" type="string" />
<add name="value4" type="string"/>
</properties>
<providers>
<clear/>
<add
name="Sql2008ProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="MyConnectionString"
applicationName="/"
/>
</providers>
</profile>
</system.web>
</configuration>

Resources