A blog of ASP.NET 2.0 Samples, Articles, Reviews and Discussions. The purpose of this blog is to provide a resource to the quick information that developers need on Microsoft's new ASP.NET 2.0.

Saturday, December 10, 2005

ASP.NET 2.0 Create a Custom Provider for web.config

I have noticed that the question of how you make a common provider for everyday things is being asked a lot. I decided to answer it. I mean its a common thing. I like SQL Express and all but if youre a developer youre going to start with SQL Dev first at least. I mean the only use I saw for SQL Express was for teaching and for nice delieverables of applications. However, I like to use the core tools as I will be required to use in the "real world". Also, I want my own personal apps using the top stuff. That being said, my example is a provider for SQL Server. It just does the normal 3 providers [ Membership,Profile ,Role ] and when I find some good documentation, ill do the rest. I have found that the best stratigy for writing a provider is in the application's own web.config. I saw that you could just stick it into machine.config; but I only recommend that If ALL your applications rely on the same settings. I personally have applications that do all kinds of things with various permissions. My example is for web.config :

First, We will start of with the connection string. This is important because all your Provider elements [ Membership,Profile,Role] will rely on when connecting to the db. You want to make sure that the user connecting has privs to connect AND operate the dbs needed. I would not recommend just giving the sa account. Take some time and provide a secure method to the DB.

Second, I assume you have installed the ASPNETDB into your database using ASP.NET SQL Registration


Please Note: Blogger is wierd with code so I maybe missing an opening '<' in my XML.

In your web.config, put a connection strings section below the


-
 <connectionStrings>
<add name="SQLServerDEV" connectionString="server=.\MSSQLDEV;uid=meebo;password=rocks;
Trusted_Connection=False;database=IMontheWEB
" providerName="System.Data.SqlClient" />
<connectionStrings>

Make sure the ASPNETDB or your custom provider points to the rite DB where it is stored.

Then add the nodes to set up the provider :

- <membership>
- <providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="SQLServerDEV" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
providers>
membership>
- <profile>
- <providers>
<add name="AspNetSqlProfileProvider" connectionStringName="SQLServerDEV" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
providers>
profile>
- <roleManager>
- <providers>
<add name="AspNetSqlRoleProvider" connectionStringName="SQLServerDEV" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
providers>
roleManager>


Now you can just switch your connection string as you scale up and youve got a custom provider should you need to give your app to someone else and they can load your db into their platform.

1 Comments:

Anonymous Anonymous said...

Thanks, but as I've understood membership class include limited number of properties to customize - requirement of secret question, length of password and presence of non-alphanumeric chars and some others...
But Explain me please, how could I add custom fields to users accounts? And how could I delete e-mail field?

Sunday, May 21, 2006 7:57:00 AM

 

Post a Comment

Links to this post:

Create a Link

<< Home