[Original address] common gotcha: Don't forget When adding providers
[Original article publication date] Monday, November 20,200 6 pm
Recently, I helped a few people, how are they working on the web. add a new member (membership) to the config file, and the role and user information (profile) Provider encounter problems. If you want to add the provider declaration in your web. config file, continue to read it and learn how to avoid a common problem.
Symptoms:
You need to configure ASP. NET 2.0 to store your member/role management/user information data in a remote SQL database. To achieve this goal, you first use the aspnet_regsql.exe tool to generate appropriate data definitions in the database. You decided not to be on your web. the Config File overwrites the connection string "localsqlserver", as shown in the following figure. register a new provider in the config file (Note: The following registration has a bug, so do not copy/paste it ):
<Membership>
<Providers>
<Add name = "aspnetsqlmembershipprovider"
Type = "system. Web. Security. sqlmembershipprovider, system. Web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"
Connectionstringname = "mydatabase"
Enablepasswordretrieval = "false"
Enablepasswordreset = "true"
Requiresquestionandanswer = "true"
Requiresuniqueemail = "false"
Passwordformat = "hashed"
Maxinvalidpasswordattempts = "5"
Minrequiredpasswordlength = "7"
Minrequirednonalphanumericcharacters = "1"
Passwordattemptwindow = "10"
Passwordstrengthregularexpression = ""
Applicationname = "/"
/>
</Providers>
</Membership>
When registering the above provider, you carefully set the applicationname attribute, thus avoiding another very common problem.
However, when you run your application on a machine without SQL Express, you see a strange behavior. You may get an error message like this:
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)(An error occurred when establishing a connection to the server. When you connect to SQL Server 2005, the connection failure may be caused by the fact that SQL server does not allow remote connection by default. (Provider: SQL network interface, error: 26-An error occurred while searching for the specified service/instance ))
You may also find that the Web management tool has problems connecting to your database, or the role/user you created using it has not been properly stored in the database configured above.
Cause:
The root cause of the above problem depends on how the new provider registers in the web. config file.
The <providers> section in the web. config file is implemented in a set, so it is possible to register multiple providers at the same time. This is useful when you use a member storage to authenticate some users, and use another member storage to authenticate another user.
By default, Asp. NET 2.0 root Web on your machine. the Config File registers a batch of default SQL express providers. During your first visit, an SQL express database will be generated in the/app_data folder of your application, to store/manage Member/role/user information data. Because this is registered at the machine range level, by default, all providers' sets inherit the registration information. Unless you explicitly set <clear/> or overwrite the inherited value, your application uses the default registered member/role/user information provider.
Because the above Web. config file simply adds a new provider,NoClear or replace the default provider registration information. The above application is now configured with two membership providers. When you call membership in encoding. when createuser (), Asp. net will try to create this user in these two member databases. If you have not installed SQL Express on your system, the creation of the user for this database will fail, cause the above errors or strange behavior.
How to solve this problem:
Unless you want to register multiple members, roles, or user information databases (this case should be rare), you should. in the config file, you can add clear <clear/> commands without exception before the <Add/> statement:
<Membership>
<Providers>
<Clear/>
<Add name = "aspnetsqlmembershipprovider"
Type = "system. Web. Security. sqlmembershipprovider, system. Web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a"
Connectionstringname = "mydatabase"
Enablepasswordretrieval = "false"
Enablepasswordreset = "true"
Requiresquestionandanswer = "true"
Requiresuniqueemail = "false"
Passwordformat = "hashed"
Maxinvalidpasswordattempts = "5"
Minrequiredpasswordlength = "7"
Minrequirednonalphanumericcharacters = "1"
Passwordattemptwindow = "10"
Passwordstrengthregularexpression = ""
Applicationname = "/"
/>
</Providers>
</Membership>
This will ensure that your application does not inherit any default provider registration settings.
Note that you must do this for every provider declaration you have registered. Therefore, if you want to add the <roles> and <profile> providers, make sure you have added the <clear/> command in their providers' section.
I hope this article will help you,
Scott
Note: click here to view previous articles on sp. Net Tips/tricks, gotchas, and recipes.