Thursday, August 5, 2010

Configuring MOSS 2007 Anonymous site to use ASP.NET Membership Provider

Hi again in this anonymous site series..

In my previous post I explained steps about how to create anonymous web site using SharePoint existing site. So in this post I am going to make use of out of the box ASP.NET Membership Provider and it to anonymous site which we created. (This is easy one)

So very first step is to have ASPNET db in SQL Server, and for this we are going to use the traditional ASP.NET way with popular command aspnet_regsql

1. Open visual studio command prompt and type in command aspnet_regsql, you will see ASP.NET Sql Server setup wizard, click next and select configure SQL Server for application services.

Enter Database Server name and keep database name as <default> in dropdown (this will create db with name aspnetdb in SQL Server), click finish on next screen

2. Once aspnetdb database is created in SQL Server, then next step is to make use of this db.

Browse to Central Administration site and click on Application Management tab. Find and click on Authentication Providers, select Internet zone. Change Authentication type to Forms , and enter Membership Provider Name as AspNetSqlMembershipProvider and Role Manager Name as AspNetSqlRoleProvider. Click on save.


3. Now we have configured anonymous application to use Forms authentication, next step is to specify Membership provider and role manager entries in web.config file of anonymous web site

Add following entries to web.config file


 
<membership>
  <providers>
   <add
name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider,System.Web,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
passwordFormat="Hashed"
applicationName="/"
/>
  </providers>
</membership>

<roleManager>
  <providers>
    <remove name="AspNetSqlRoleProvider" />

<add
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider,System.Web,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"
role="SignedInClient"
applicationName="/"
/>
   </providers>
</roleManager>

<connectionStrings>
   <remove name="LocalSqlServer" />

<add name="LocalSqlServer"
connectionString="Data Source=DbServer;Initial Catalog=aspnetdb;
Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Connection string is required for application to know about aspnetdb.

4. Now , to register users with anonymous site you can use out of box ASP.NET user registration wizard control and Login control , so there are two ways to achieve this
a. You can create your custom page layouts and add those ASP.NET controls to page layouts like this

For Login Page:
<asp:Login runat="server" ID="Login1" DestinationPageURL="/Pages/Default.aspx" CreateUserText="Create User" CreateUserUrl="/Pages/Registration.aspx"></asp:Login>
For Registration Page:
<asp:CreateUserWizard ID="CreateUserWizard2" runat="server" ContinueDestinationPageUrl="/Pages/Default.aspx">
            <WizardSteps>
       <asp:CreateUserWizardStep ID="CreateUserWizard1" runat="server" />
       <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
            </WizardSteps>
 </asp:CreateUserWizard>


b. Or you can create your web parts wrapping OOB ASP.NET controls and add them to your site

Login WebPart :
             protected override void CreateChildControls()
            {
            try
            {
                base.CreateChildControls();
                Login loginControl = new Login();
                loginControl.DestinationPageUrl = "/Pages/Default.aspx";
                loginControl.CreateUserUrl = "/Pages/Registration.aspx";
                this.Controls.Add(loginControl);
            }
            catch (Exception ex)
            {
               
   }
Registration WebPart:
protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                CreateUserWizard cwz = new CreateUserWizard();
                cwz.ContinueDestinationPageUrl = "/Pages/Default.aspx";
                this.Controls.Add(cwz);

            }
            catch (Exception ex)
            {
               
            }
        }


Note: there are many properties of these controls which you can configure as your need



5. Once you have set up all these things and have your custom login page then , change default login page of anonymous site using IIS Manager like this

Find anonymous site in IIS > find authentication in features view >right click on Forms Authentication> click edit > change login url



Above all settings worked pretty perfectly for me (at least :) )

Reference best Links: This and This