FORMS AUTHENTICATION.pdf

(246 KB) Pobierz
http://www.simple-talk.com/content/print.aspx?article=389
Configuring Forms Authentication in SharePoint 2007
Strona 1 z 12
Configuring Forms Authentication in SharePoint 2007
08 May 2007
by Damon Armstrong
SharePoint 2007 is the latest release of Microsoft's enterprise collaboration suite, which
tightly integrates with the Microsoft Office Suite and allows organizations to establish well-
managed corporate knowledge from the darkest depths of informational chaos. At least
that's Microsoft unbiased opinion. In my experience, SharePoint 2007 is a major
improvement over its predecessor, but it still takes a bit of know-how to make it work.
The latest rendition of SharePoint is built on top of ASP.NET 2.0, so ASP.NET developers
should feel right at home developing against, and customizing, SharePoint 2007. In fact,
some of the "latest technologies" in SharePoint, like Master Pages and Forms
Authentication, are "not-quite-the-latest technologies" from ASP.NET. In this article, I'll
cover some of the quirks to Forms Authentication that you will doubtless encounter when
trying to set it up in SharePoint.
A step-by-step guide to configuring Forms
authentication in SharePoint 2007
Following is a checklist for setting up Forms Authentication in SharePoint 2007
1. Setup the membership data store
2. Add a new user to the membership data store
3. Configure SharePoint Central Administration web.config
4. Configure the SharePoint site's web.config
5. Enable Forms authentication on the SharePoint site
6. Authorize the Forms-based user to access the site
7. Login
In this article, we will be using the SQL Server membership provider to authenticate
users, but you can use any membership provider that you so choose. The steps involved
will be about same, but the specifics of those steps may change depending on your
provider. I'm also assuming that you've already installed SharePoint and created the
SharePoint site on which you're trying to enable forms authentication.
Step 1: Setup the membership data store
Before you can use the SQL Server membership provider, you have to set up the database
that the provider uses to store member and role information. Microsoft ships a handy tool
named the ASP.NET SQL Server Setup Wizard along with the .NET Framework, which will
guide you through the process of creating the table structure and stored procedures
required for the provider. You can launch the wizard by running aspnet_regsql.exe from
the .NET Framework folder, which is normally found in the following location:
<WindowsDirectory>\Microsoft.NET\Framework\<version>\aspnet_regsql.exe
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
When you launch the wizard, the "Welcome" screen appears and tells you all sorts of useful
things about what the wizard does and the command line parameters you can use to get
more options. It makes for great reading. When you've satisfied your literary pallet, click the
Next button to display the "Select a Setup Option" screen (Figure 1).
http://www.simple-talk.com/content/print.aspx?article=389
2008-03-10
53711684.004.png
Configuring Forms Authentication in SharePoint 2007
Strona 2 z 12
Figure 1 – ASP.NET SQL Server Setup Wizard – Select a Setup Option screen
From the "Select a Setup Option" screen, choose the "Configure SQL Server for application
services" option button. This lets the wizard know you want to add new tables and stored
procedures to a membership database. You can also use the wizard to remove the table
structure and delete all data in the database, but we don't need to deal with that right now.
If you accidentally add the structure to the wrong dataset, you may have to deal with it later.
Click "Next" to move to the "Select the Server and Database" screen (Figure 2).
http://www.simple-talk.com/content/print.aspx?article=389
2008-03-10
53711684.005.png 53711684.006.png
Configuring Forms Authentication in SharePoint 2007
Strona 3 z 12
Figure 2 – ASP.NET SQL Server Setup Wizard – Select the Server and Database
screen
Enter the name of your database server in the Server textbox to let the wizard know which
SQL Server it needs to access. Then enter or select a database name in the Database
combo box. The combo box displays a drop down containing a list of existing databases. If
you want to add the tables and stored procedures for the provider to an existing database,
select the database from the list. If you want to create a new database, then just type the
name of the new database directly in the combo box and the wizard will create the
database automatically. You may also need to enter SQL Server authentication credentials
if you connect to the database using SQL Server authentication instead of Windows
authentication. These credentials are not used outside of the wizard, so it won't affect your
SharePoint configuration one way or the other. Click the Next button to continue to the
"Confirm Your Settings" screen.
The "Confirm Your Settings" screen displays a summary of the epoch-defining choices
you've made thus far in the wizard. In other words, the server and database name. If you're
feeling hesitant about either, then this is your chance to back out. When you've got your
courage built up, click the Next button.
In about a second, or about one and half seconds if you're using a Virtual PC image (like
me), the wizard creates all of the tables and stored procedures required by the membership
provider. If it takes longer than that, you've entered a setting incorrectly and the wizard is
waiting to time out (or you have a really slow machine). The wizard then displays a final
status screen indicating success or failure. If the wizard fails, it details the reasons why so
you can fix the problem. There are only six settings in the entire wizard (if you count option
buttons as "settings") so you should have a sporting chance at troubleshooting the problem.
The success screen just tells you that everything worked and to click the Finish button.
http://www.simple-talk.com/content/print.aspx?article=389
2008-03-10
53711684.007.png 53711684.001.png
Configuring Forms Authentication in SharePoint 2007
Strona 4 z 12
At this point, the database you selected is populated with the proper table structure and
stored procedures required by the provider, so now you can add a user to the membership
database.
Step 2: Add a user to the membership data store
In IIS 7.0, there is a convenient "Add User" feature that uses the membership provider
configured for the website to create a user. Unfortunately, IIS 7.0 isn't available for
Windows Server 2003 so, in a production environment, you're probably stuck with IIS 6.0,
which doesn't have a comparable add user feature. This makes adding users a bit tedious,
but here's how you do it.
1. Create a new ASP.NET web application
2. Configure the new application for Forms authentication and point it at your newly-
created membership database
3. Copy the machine key element from your SharePoint site's Web.config into to your
new web application
4. Add users and roles using the ASP.NET Web Site Administration Tool (if you have
Visual Studio 2005 handy) or create users via the CreateUserWizard ASP.NET
control.
I'm assuming you know how to create a new web site, so I'm not delving into any of the
specifics of step 1. Once you have the website created, add a new Web.config to the
application root and add the following configuration setting to the file:
Listing 01 – Web.config for the User Creation Website
<?xml version="1.0"?>
<configuration xmlns=" http://schemas.microsoft.com/.NetConfiguration/v2.0 ">
<connectionStrings>
<add name="MembershipDatabaseCNX" connectionString="SERVER=localhost;
DATABASE=MembershipDatabase; TRUSTED_CONNECTION=true;"/>
</connectionStrings>
<system.web>
<machineKey
validationKey="8E074B186056F889587355255B167DA297AD837E43FD9850"
decryptionKey="991D4DEB57A2263855C31AA1D3FF4F1AD508A53D2A94658F"
validation="SHA1"
/>
<authentication mode="Forms"/>
<membership defaultProvider=" DemoMembershipProvider ">
<providers>
<add
name=" DemoMembershipProvider "
type="System.Web.Security.SqlMembershipProvider,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="MembershipDatabaseCNX "
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="7"
minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
http://www.simple-talk.com/content/print.aspx?article=389
2008-03-10
53711684.002.png
Configuring Forms Authentication in SharePoint 2007
Strona 5 z 12
/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider=" DemoRoleProvider ">
<providers>
<add
name=" DemoRoleProvider "
connectionStringName="MembershipDatabaseCNX"
applicationName="/"
type="System.Web.Security.SqlRoleProvider, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
/>
</providers>
</roleManager>
</system.web>
</configuration>
I've bolded a few areas of Listing 01 because you will need to modify them to work on your
system:
1. Replace the machineKey element from the listing with the machine key element
in the Web.config from your SharePoint site . The machine key from the listing is
the machineKey from my SharePoint site (on a VPC local to my box, so calm down
you crazy Hax0rs) so it won't do you much good. The machineKey element changes
from site to site, so make sure you get it from the site you want to configure for Forms
authentication and not another site, or the SharePoint Central Administration site. You
need matching machineKeys in the web application and the SharePoint site because
user passwords are hashed (one way encrypted) and the hash routine uses the
machine key value as part of the hashing algorithm.
2. Make sure your connection string points at the appropriate server that houses the
membership database you just created. Also make sure the appropriate credentials
are supplied to the connection string.
3. You can name your connection string anything you want, just make sure you use the
same name later on in the connectionStrngName parameter for the membership
and roleManager provider configurations.
4. Make sure your applicationName parameters match in both the membership and
roleManager provider configurations. The SqlMembershipProvider allows multiple
applications to use the same membership database, so a mismatched name makes
the provider think there are two applications instead of one and your members and
roles won't associate correctly.
5. Feel free to configure the password settings of the membership provider as you see
fit.
Once you have the configuration settings in place for your web application, you need a way
to add users. If you are using Visual Studio 2005, you can use the built-in Web Site
Administration Tool:
1. Click the Website menu and choose the ASP.NET Configuration menu item. This
launches a new web browser window that displays the Web Site Administration Tool.
2. Click on the Security tab or link.
3. Click on the Create User link and create a new user. Remember the login information
because you'll be needing it later.
If you do not have Visual Studio 2005, then you can use the CreateUserWizard control to
add a new user to the membership database. It's not as nice as the Web Site
Administration Tool interface, but it does get the job done. Create a new page named
http://www.simple-talk.com/content/print.aspx?article=389
2008-03-10
53711684.003.png
Zgłoś jeśli naruszono regulamin