Introduction
In the first part we discuss in detail how to move our database which contains the membership tables from the default aspnetdb Database to our database.
In this part we will show how to change the default settings of asp.net membership and role providers to read from tables which stored on our database.
Prerequisites
- Visual Studio
Overview
ASP.net 2 is shipped with default membership providers and roles provider classes, the default settings of asp.net 2 web site is to read from these providers which was stored on the aspnetdb database.
To successfully change the default settings of providers to your providers,you have to:
- Change the Default Connection to SQL Express Instance.
- Change the membership provider.
- Change the roles provider.
- Change the Personalization provider.
Membership provider is responsible for storing the membership users and their roles.
Roles Provider is responsible for Role management.
And Personalization provider is responsible for personalization in the web site and web parts.
A) Change the default connection to SQL Express Instance:
By default, the asp.net web site when tries to connect to a database it search first to the installed sqlExpress on the machine, that's why you have to change the default connection string to direct the web site to read from the connection string element on the web.config.
First, open your web.config and write this element on the connection string element.
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="MyDBConnectionString" connectionString="Data Source=.;Initial Catalog=MYDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
On this section remove LocalSqlServer means that you want to point to the installed SQL Server instance not the SQL Express Instance.
B) Change the default membership provider:
Change the default membership element to point to your connection string not to the SqlExpress default connection string.
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add connectionStringName="MyDBConnectionString" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/"
requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10" passwordStrengthRegularExpression=""
name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</membership>
On connection string element of the membership element, you have to edit this element and write the key of your connection string in the web.config.
C) Change the default role provider:
Before do this step, take care that Role provider should be enabled, you can enable role provider from asp.net configuration tool.
After you enable the role manager, you will notice that Role Manager element was added to your web.config and enabled=true .
<roleManager enabled="true">
<providers>
<remove name="AspNetSqlRoleProvider" />
<add connectionStringName="MyDBConnectionString" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<remove name="AspNetWindowsTokenRoleProvider" />
</providers>
</roleManager>
Like you did on the membership provider, do the same on the role manager by edit the connection string by type the key of the connection string you want to use.
D) Change the default personalization provider:
This step is required if your web site contains Web Parts only, if your web site contains web parts you have to edit the personalization element on the web.config.
<webParts>
<personalization defaultProvider="AspNetSqlPersonalizationProvider">
<providers>
<remove name="AspNetSqlPersonalizationProvider" />
<add name="AspNetSqlPersonalizationProvider"
type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
connectionStringName="MyDBConnectionString"
applicationName="/" />
</providers>
</personalization>
</webParts>
By changing the above 4 provider then your website have to be ready to deploying to the production server, at this mammon all the providers such as membership, role, personalization was configured directly to load data from your database.
rupak dasPosted Jul 8, 2010, 8:06 AM
what changes I need to make in my web.config to deploy my website in web server? My web.config code is as follows: <?xml version="1.0"?><configuration> <system.web> <compilation debug="true"> <assemblies> <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="eWorld.UI, Version=2.0.6.2393, Culture=neutral, PublicKeyToken=24D65337282035F2"/> <add assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.Shared, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.Enterprise.Framework, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.Enterprise.Desktop.Report, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/><add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/></assemblies></compilation> <authentication mode="Windows"/> <identity impersonate="true" userName="Administrator" password="techno"/> <httpHandlers><add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/></httpHandlers></system.web> <appSettings> <add key="ConnectionInfo" value="server=(local);database=infotechsolution;Integrated Security=SSPI" /> <add key="CrystalImageCleaner-AutoStart" value="true" /> <add key="CrystalImageCleaner-Sleep" value="60000" /> <add key="CrystalImageCleaner-Age" value="120000" /> </appSettings> </configuration> Please help ...
Sa AleditedPosted Jul 4, 2010, 5:50 PMEdited Jul 4, 2010, 5:54 PM
ya know,, after 5 years you still saving life !! You just saved mine man! i spent more then 2 weeks my problem solved after readying your article Thanks man :) ( Allah y36eek el 3afia )
Barani GopalPosted Oct 3, 2009, 5:35 AM
Hello, How will you maintain versioning in asp .net web application framework 2.0. pls gives your answer. BaranigopalU
dimetholetherPosted Oct 10, 2007, 4:45 PM
Would this configuration changes work with SQL 2000 as well.
Rahman MarikareditedPosted Dec 11, 2006, 11:21 AMEdited Jan 11, 2007, 12:35 AM
How do i remove the registry value. where do i start? Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: Could not load file or assembly 'System.Web, Version=2.0.0.0, Culture=neutral' or one of its dependencies. The system cannot find the file specified. Source Error: Line 54: <remove name="AspNetSqlRoleProvider" /> Line 55: <add connectionStringName="PropertiesConnectionString" applicationName="/" Line 56: name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral" /> Line 57: <remove name="AspNetWindowsTokenRoleProvider" /> Line 58: </providers> Source File: D:\Clients\h&DDev\hdInvestmentsWeb\web.config Line: 56 Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web, Version=2.0.0.0, Culture=neutral' could not be loaded. WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. Please help. Regards Rahman <configuration> <system.windows.forms jitDebugging="true" /> </configuration> ffffffffffff
JonathanPosted Dec 1, 2006, 3:21 PM
While going through your tutorial, on Part 2 I did not have anything in there for <membership>, do I still have to put that block of go in my web.config file? Also, I just had <roleManager enabled="true"> in my file. Do I need to add the whole roleManager block that you have shown in my file as well? I made my web.config file look very similar to what you have shown in your tutorial but it still won't read the SQL Server. When I make the changes in the tutorial will I still be able to use the configuration tool to add/remove user and assign roles like I would when it is using SQLExpress? Here is what my web.config file looks like: <?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <connectionStrings> <add name="aspnet_staterKits_TimeTracker" connectionString="Data Source=servername;Initial Catalog=TimeTracker;uid=sa;pwd=password" providerName="System.Data.SqlClient"/> <remove name="LocalSqlServer"/> <add name="TimeTrackerConnectionString" connectionString="Data Source=servername;Initial Catalog=TimeTracker;uid=sa;pwd=password" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <siteMap defaultProvider="AspNetXmlSiteMapProvider" enabled="true"> <providers> <clear/> <add name="AspNetXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" siteMapFile="web.sitemap" securityTrimmingEnabled="true"/> </providers> </siteMap> <authentication mode="Forms"> <forms loginUrl="~/TimeTracker/login.aspx"/> </authentication> <compilation debug="true"/> <customErrors mode="RemoteOnly"/> <machineKey decryptionKey="AutoGenerate,IsolateApps"/> <membership> <providers> <remove name="AspNetSqlMembershipProvider" /> <add connectionStringName="TimeTrackerConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </providers> </membership> <roleManager enabled="true"/> <!--<providers> <remove name="AspNetSqlRoleProvider" /> <add connectionStringName="TimeTrackerConnectionString" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <remove name="AspNetWindowsTokenRoleProvider" /> </providers> </roleManager>--> </system.web> </configuration>
steve bothamPosted Nov 3, 2006, 11:30 PM
I knew nothing about this before I read your post. It was easy to follow and worked perfectly, thanks steve
Tina BhatePosted Oct 27, 2006, 11:30 AM
My email:- [email protected] An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) I am getting this error please help me out.....My DB is on remote server and my website is on remote server (different machines). Thanks bye
soumya seditedPosted May 5, 2006, 11:24 AMEdited Dec 13, 2006, 3:16 PM
Hi, I am struggling with SQL Server connection problem after I deploy my ASP.NET appl. on to remote server. I followed all you instructions upto editing web.config. I cannot find a web.config where there are entries for "connectionStrings" etc. That is part of machine.config on my local and remote systems. So, I edited my local Windows\Microsoft.NET\framework\..\machine.config by pasting the lines from your article and published the appln. again. Publishing went on ok, but I still get the same error on the accessing the website as before. The error apparently is because of SQL Server not allowing remote connections. But I have allowed remote connections using Surface manager on the server. Below is the error I get. any clue ? Thanks in advance, soumya ------------------------------------ An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [SqlException (0x80131904): An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +735107 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188 System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup) +820 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +628 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +170 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +130 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +424 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +496 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +82 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105 System.Data.SqlClient.SqlConnection.Open() +111 System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +84 System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +197 System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider.GetConnectionHolder() +16 System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider.LoadPersonalizationBlobs(WebPartManager webPartManager, String path, String userName, Byte[]& sharedDataBlob, Byte[]& userDataBlob) +195 System.Web.UI.WebControls.WebParts.PersonalizationProvider.LoadPersonalizationState(WebPartManager webPartManager, Boolean ignoreCurrentUser) +95 System.Web.UI.WebControls.WebParts.WebPartPersonalization.Load() +105 System.Web.UI.WebControls.WebParts.WebPartManager.OnInit(EventArgs e) +497 System.Web.UI.Control.InitRecursive(Control namingContainer) +321 System.Web.UI.Control.InitRecursive(Control namingContainer) +198 System.Web.UI.Control.InitRecursive(Control namingContainer) +198 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +692 Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 <!-- [SqlException]: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) at System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) at System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider.GetConnectionHolder() at System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider.LoadPersonalizationBlobs(WebPartManager webPartManager, String path, String userName, Byte[]& sharedDataBlob, Byte[]& userDataBlob) at System.Web.UI.WebControls.WebParts.PersonalizationProvider.LoadPersonalizationState(WebPartManager webPartManager, Boolean ignoreCurrentUser) at System.Web.UI.WebControls.WebParts.WebPartPersonalization.Load() at System.Web.UI.WebControls.WebParts.WebPartManager.OnInit(EventArgs e) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\5dfbaf18\48ab4cc4\App_Web_jzazoggo.0.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) --><!-- This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using <customErrors mode="Off"/>. Consider using <customErrors mode="On"/> or <customErrors mode="RemoteOnly"/> in production environments.-->
Rusty MartinPosted Apr 28, 2006, 1:58 PM
I have been struggling with this exact solution for a few weeks. Thanks for putting it together. Great job! Just a quick thought, what happens when you try to make changes on your development server to the original project such as adding roles or making changes to the setup for authentication? Does the development environment look to the new connection or back to the original .mdf datasource?