Monday, December 29, 2008

SharePoint 2007 Forms Authentication

It’s easy when you know how! I’ve been playing around with Beta 2 of Microsoft Office SharePoint Server 2007 for a couple of weeks now trying to get Forms Authentication to work. Together with Dan Winter from Microsoft and Adam Tybor I’ve got an internet presence site working with Forms Authentication and anonymous access. Perfect for an public facing internet site run on SharePoint 2007. So here’s the step by step guide on how to do it (with pictures!)
A quick word of warning – this works for Moss 2007 Beta 2. If the process for any subsequent releases change I’ll post the changes on the blog as well.
1, Create a new site – go to Office Server Central Administration -> Application Management -> Create or Extend Web Application -> Create a new Web Application
Enter the settings to create a new SharePoint site. Leave the Authentication Provider as NTML. Check Allow Anonymous to Yes. Fill in the rest of the settings as you normally would and create the Web Application.

2, Once the Application has been created choose to ‘Create a new Windows SharePoint Services site collection’. Give the site a nice name, check the box to ‘Create site at this url’ and leave (root) selected from the dropdown. For the primary site administrator enter the username of the main SharePoint administrator. This will change anyway once we’ve got Forms Authentication up and running. From the Template Selection pick ‘Internet Presence Web Site’. This is a good one as it will have a login screen created by default that we can use to let people login under Forms Authentication. Click ok to finish creating the site, and in the following screen click the link to open the new site in a new browser.

2a, I missed this step out originally - sorry
Go back to the Application Management page in Central Admin and click on Authentication Providers. In the following screen select the site you wish to change the authentication provider to Forms for. Once you've selected it you'll see Windows as the MemberShipProviderName. Click on this and you'll see the screen below:

Set the authentication type to Forms and enter the Member Ship Provider Name to AspNetSqlMembershipProvider. If anonymous access isn't ticked, tick that now.
3, Now we need to finish enabling anonymous access. When you visit the site for the first time by clicking the link in Central Admin it will ask you to login with normal Windows Login popup (login with the username you added in step 2 for the site administrator). Assuming you created an Internet Presence site you’ll get a list of tasks to do on the front page with ‘Enable anonymous access’ being the top one.

Click the link and it’ll take you to the page where you set what access visitors to the site will have. For this example just choose Entire Website and click ok.

4, The next step is outside of SharePoint. We need to create our aspnetdb to store all our roles and users in. We could use the database that gets installed by SharePoint if you do the default installation, but to keep it on a separate db I installed SQL Express and the Management studio for it. Download them both from here.
Once you’ve got these installed fire up a Visual Studio command prompt (or a cmd prompt and go to c:\windows\microsoft.net\framework\v2.0.50727) and run aspnet_regsql. Create your roles and membership data on the sql server you so wish.

5, Now you need to edit web.config for the site you created and web.config for the SharePoint v3.0 Central administration website. These sites are created by default in c:\inetpub\wwwroot\wss\virtualdirectories. (To find out which directory is your Central admin one you’ll need to go into IIS, right click the site->properties->home directory, same for the other if you didn’t change the name of the folder when creating the site)
So in both web.configs we need to supply the connection string details for our aspnetdb. Just outside the tag put:




Once you’ve edited both web.config’s go to a command prompt and do an iisreset.
6, Now we need to add some users to our database. The easiest way to do this is to create an ASP.NET 2.0 website using Visual Studio 2005 (or Visual Web Dev). Add a web.config to the project and then add exactly the same connection string as you did above for your new SharePoint site. Build the project.
Back in Visual Studio, under the website menu, select the option for ASP.NET Configuration. This opens up the ASP.NET web site administration tool where we can add the users and roles we want.

Select the Security link, create a role (something like administrators), and then go and add a user.
7, Now the final step, and this is why we had to add the connection string to the web.config for the Central Administration site as well. The site administrator that we added when we created the site (which was an active directory account) has now been removed. So we need to add the user we created in step 6 as the site collection administrator.
Goto Central Administration -> Application Management -> site collection administrators – and enter the username created in step 6 in Primary site collection administrator.

And that’s it. Logout of Central Administration (make sure you do this as it saves a cookie whenever you are logged in), and navigate to your new Forms Authentication site. You should be able to anonymously access it ok. Click the login link at the top right of the screen and use the username and password you created to login! Woot!!!! :-)

Print this post

No comments: