1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional ASP.NET 3.5 in C# and Visual Basic Part 80 pdf

10 331 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 163,24 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Configuring the Provider for SQL Server 2005 After you have set up your SQL Server database for the personalization system, the next step is to redefine the personalization provider so t

Trang 1

If you worked with the SQL Server personalization provider using SQL Server Express files as explained earlier, you probably found it easy to use The personalization provider works right out of the box —

without any set up or configuration on your part Using the SQL Server personalization provider with a full-blown version of SQL Server, however, is a bit of a different story Although it is not difficult to work with, you must set up and configure your SQL Server before using it

ASP.NET 3.5 provides a couple of ways to set up and configure SQL Server for the personalization

framework One way is through the ASP.NET SQL Server Setup Wizard, and the other method is by

running some of the SQL Server scripts provided with the NET Framework 2.0

Using the ASP.NET SQL Server Setup Wizard is covered in detail in Chapter 12.

To use this wizard to set up your SQL Server for the ASP.NET 3.5 personalization features, you must

first open up the aspnet_regsql.exe tool by invoking it from the Visual Studio 2008 Command Prompt

You open this command prompt by selecting Start ➪ All Programs ➪ Visual Studio 2008 ➪ Visual Studio Tools ➪ Visual Studio 2008 Command Prompt At the prompt, type inaspnet_regsql.exeto open the

GUI of the ASP.NET SQL Server Setup Wizard If you step through the wizard, you can set up your SQL Server instance for many of the ASP.NET systems, such as the personalization system

Using SQL Scripts to Install Personalization Features

Another option is to use the same SQL scripts that these tools and wizards use If you look atC:\WINDOWS\ Microsoft.NET\Framework\v2.0.50727\, from this location, you can see the install and remove

scripts —InstallPersonalization.sqlandUninstallPersonalization.sql Running these scripts

provides your database with the tables needed to run the personalization framework Be forewarned that you must run theInstallCommon.sqlscript before running the personalization script (or any of the new other ASP.NET system scripts)

Configuring the Provider for SQL Server 2005

After you have set up your SQL Server database for the personalization system, the next step is to

redefine the personalization provider so that it works with this instance (instead of with the default

Microsoft SQL Server Express Edition files)

You accomplish this in theweb.configfile of your application Here, you want to configure the provider and then define this provider instance as the provider to use Listing 15-24 shows these additions plus

the enlarged<profile>section of theweb.configfile

Listing 15-24: Connecting the SqlProfileProvider to SQL Server 2005

<configuration>

<connectionStrings>

<add name="LocalSql2005Server"

connectionString="data source=127.0.0.1;Integrated Security=SSPI" />

</connectionStrings>

<profile defaultProvider="AspNetSql2005ProfileProvider">

<providers>

Continued

747

Trang 2

<clear />

<add name="AspNetSql2005ProfileProvider"

connectionStringName="LocalSql2005Server" applicationName="/"

type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral,

PublicKeyToken=b03f5f7f11d50a3a" />

</providers>

<properties>

<add name="FirstName" />

<add name="LastName" />

<add name="LastVisited" />

<add name="Age" />

<add name="Member" />

</properties>

</profile>

</configuration>

The big change you make to this profile definition is to use thedefaultProviderattribute with a value

that is the name of the provider you want to use — in this case the newly created SQL Server provider,

AspNetSql2005ProfileProvider You can also make this change to themachine.configfile by changing

the<profile>element, as shown in Listing 15-25

Listing 15-25: Using SQL Server as the provider in the machine.config file

<configuration>

<system.web>

<profile enabled="true" defaultProvider="AspNetSql2005ProfileProvider">

</profile>

</system.web>

</configuration>

This change forces each and every application that resides on this server to use this new SQL Server

provider instead of the default SQL Server provider (unless this command is overridden in the

applica-tion’sweb.configfile)

Using Multiple Providers

You are not limited to using a single data store or provider Instead, you can use any number of

providers You can even specify the personalization provider for each property defined This means

that you can use the default provider for most properties, as well as allowing a few of them to use an

entirely different provider (see Listing 15-26)

Trang 3

Listing 15-26: Using different providers

<configuration>

<system.web>

<profile

defaultProvider="AspNetSqlProvider">

<properties>

<add name="FirstName" />

<add name="LastName" />

<add name="LastVisited" />

<add name="Age" />

<add name="Member" provider="AspNetSql2005ProfileProvider" />

</properties>

</profile>

</system.web>

</configuration>

From this example, you can see that a default provider is specified —AspNetSqlProvider Unless

specified otherwise, this provider is used The only property that changes this setting is the property

Member TheMemberproperty uses an entirely different personalization provider In this case, it employs the Access provider (AspNetSql2005ProfileProvider) through the use of theproviderattribute of the

<add>element With this attribute, you can define a specific provider for each and every property that

is defined

Managing Application Profiles

When you put into production an ASP.NET application that uses profile information, you quickly realize that you need a way to manage all the profile information collected over the lifecycle of the application

As you look at the ASP.NET MMC snap-in or the ASP.NET Web Site Administration Tool, note that

neither of these tools gives you a way to delete a specific user’s profile information or even to cleanse a database of profile information for users who haven’t been active in awhile

ASP.NET 3.5 gives you the means to manage the profile information that your application stores This is done through the use of theProfileManagerclass available in NET

Through the use of theProfileManagerclass, you can build-in the administration capabilities to

completely manage the profile information that is stored by your application In addition to being able

to access property values, such as the name of the provider being used by the personalization system or the name of the application in question, you also have a large number of methods available in the Pro-fileManagerclass to retrieve all sorts of other information concerning your user’s profile Through the

ProfileManagerclass, you also have the capability to perform actions on this stored profile information including cleansing the database of old profile information

749

Trang 4

Properties of the ProfileManger Class

The properties of theProfileManagerclass are detailed in the following table

Properties Description

ApplicationName Gets or sets the name of the application to work with

AutomaticSaveEnabled Gets or sets a Boolean value indicating whether the profile information is

stored at the end of the page execution

Enabled Gets or sets a Boolean value indicating whether the application is able to use

the personalization system

Provider Gets the name of the provider being used for the personalization system

Providers Gets a collection of all the providers available for the ASP.NET application

You can see that these properties include a bit of information about the personalization system and the

providers available to it that you can integrate into any management system you build Next, this chapter

looks at the methods available for theProfileManagerclass

Methods of the ProfileManager Class

A good number of methods are available to theProfileManagerclass that help you manage the profiles

of the users of your application These methods are briefly described in the following table

Properties Description

DeleteInactiveProfiles Provides you with the capability to delete any profiles that

haven’t seen any activity for a specified time period

DeleteProfile Provides you with the capability to delete a specific profile

DeleteProfiles Provides you with the capability to delete a collection of

profiles

FindInactive Profiles ByUserName Provides you with all the inactive profiles under a specific

username according to a specified date

FindProfilesBy UserName Provides you with all the profiles from a specific username

GetAllInactiveProfiles Provides you with all the profiles that have been inactive since

a specified date

GetAllProfiles Provides you with a collection of all the profiles

GetNumberOf InactiveProfiles Provides you with the number of inactive profiles from a

specified date

GetNumberOfProfiles Provides you with the number of total profiles in the system

As you can see from this list of methods, you can do plenty to manage to the profile information that is

Trang 5

Next, this chapter looks at building a profile manager administration page for your ASP.NET

application This example builds it as an ASP.NET page, but you can just as easily build it as a

console application

Building the ProfileManager.aspx Page

To create a simple profile manager for your application, create a single ASP.NET page in your application calledProfileManager.aspx You use this page to manage the profiles that are stored in the database for this particular application

This page includes a number of controls, but the most important is a DropDownList control that holds all the usernames of entities that have profile information in the database You might see the same username

a couple of times depending on what you are doing with your application Remember that a single user can have multiple profiles in the database

Using the DropDownList control, you can select a user and see information about his profile stored

in your data store From this page, you can also delete his profile information You can actually

perform very many operations with theProfileManagerclass, but this is a good example of some

basic ones

The code for theProfileManager.aspxpage is presented in Listing 15-27

Listing 15-27: The ProfileManager.aspx page

VB

<%@ Page Language="VB" %>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If (DropDownList1.Items.Count = 0) Then WriteDropdownList()

WriteUserOutput() End If

End Sub

Protected Sub DeleteButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs)

ProfileManager.DeleteProfile(DropDownList1.Text.ToString()) DropDownList1.Items.Clear()

WriteDropdownList() WriteUserOutput() End Sub

Protected Sub SelectButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs)

WriteUserOutput() End Sub

Protected Sub WriteUserOutput()

Dim outputInt As Integer

Continued

751

Trang 6

Dim pic As ProfileInfoCollection = New ProfileInfoCollection() pic = ProfileManager

FindProfilesByUserName(ProfileAuthenticationOption.All, _ DropDownList1.Text.ToString(), 0, 1, outputInt)

DetailsView1.DataSource = pic DetailsView1.DataBind() End Sub

Protected Sub WriteDropdownList()

Dim outputInt As Integer Dim pic As ProfileInfoCollection = New ProfileInfoCollection() pic = ProfileManager.Provider

GetAllProfiles(ProfileAuthenticationOption.All, 0, 10000, outputInt) For Each proInfo As ProfileInfo In pic

Dim li As ListItem = New ListItem() li.Text = proInfo.UserName.ToString() DropDownList1.Items.Add(li)

Next Label1.Text = outputInt.ToString() End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>ProfileAdmin Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<b>Profile Manager<br />

</b>

<br />

Total number of users in system:

<asp:Label ID="Label1" runat="server"></asp:Label><br />

&nbsp;<br />

<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>&nbsp;

<asp:Button ID="SelectButton" runat="server"

OnClick="SelectButton_Click"

Text="Get User Profile Information" /><br />

<br />

<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4"

ForeColor="#333333" GridLines="None"

Height="50px">

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#7C6F57" />

<PagerStyle BackColor="#666666" ForeColor="White"

HorizontalAlign="Center" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

Continued

Trang 7

<CommandRowStyle BackColor="#C5BBAF" Font-Bold="True" />

<RowStyle BackColor="#E3EAEB" />

<FieldHeaderStyle BackColor="#D0D0D0" Font-Bold="True" />

</asp:DetailsView>

<br />

<asp:Button ID="DeleteButton" runat="server"

Text="Delete Selected User’s Profile Information"

OnClick="DeleteButton_Click" />

</div>

</form>

</body>

</html>

C#

<%@ Page Language="C#" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

if (DropDownList1.Items.Count == 0)

{

WriteDropdownList();

WriteUserOutput();

}

}

protected void DeleteButton_Click(object sender, EventArgs e)

{

ProfileManager.DeleteProfile(DropDownList1.Text.ToString());

DropDownList1.Items.Clear();

WriteDropdownList();

WriteUserOutput();

}

protected void SelectButton_Click(object sender, EventArgs e)

{

WriteUserOutput();

}

protected void WriteUserOutput()

{

int outputInt;

ProfileInfoCollection pic = new ProfileInfoCollection();

pic = ProfileManager.FindProfilesByUserName

(ProfileAuthenticationOption.All,

DropDownList1.Text.ToString(), 0, 1, out outputInt);

DetailsView1.DataSource = pic;

DetailsView1.DataBind();

}

protected void WriteDropdownList()

Continued

753

Trang 8

int outputInt;

ProfileInfoCollection pic = ProfileManager.Provider.GetAllProfiles (ProfileAuthenticationOption.All, 0, 10000, out outputInt);

foreach (ProfileInfo proInfo in pic) {

ListItem li = new ListItem();

li.Text = proInfo.UserName.ToString();

DropDownList1.Items.Add(li);

} Label1.Text = outputInt.ToString();

}

</script>

Examining the Code of ProfileManager.aspx Page

As you look over the code of theProfileManager.aspxpage, note that theProfileManagerclass is used

to perform a couple of different operations

First, theProfileManagerclass’sGetAllProfiles()method is used to populate the DropDownList

control that is on the page The constructor of this method is presented here:

GetAllProfiles(

authenticationOption,

pageIndex,

pageSize,

totalRecords)

TheGetAllProfiles()method takes a number of parameters, the first of which allows you to define

whether you are using this method for all profiles in the system, or just the anonymous or authenticated

user’s profiles contained in the system In the case of this example, all the profiles are retrieved with this

method This is accomplished using theProfileAuthenticationOptionenumeration Then, the other

parameters of theGetAllProfiles()method require you to specify a page index and the number of

records to retrieve from the database There is not a get all option (because of the potential size of the data

that might be retrieved); so instead, in this example, I specify the first page of data (using0) and that this

page contains the first 10,000 records (which is basically a get all for my application) The last parameter

of theGetAllProfiles()method enables you to retrieve the count of the records if you want to use that

anywhere within your application or if you want to use that number to iterate through the records The

ProfileManager.aspxpage uses this number to display within theLabel1server control

In return from theGetAllProfiles()method, you get aProfileInfoCollectionobject, which is a

collection ofProfileInfoobjects Iterating through all theProfileInfoobjects in the

ProfileIn-foCollection, you are able to pull out the some of the main properties for a particular user’s profile

information In this example, just theUserNameproperty of theProfileInfoobject is used to populate

the DropDownList control on the page

When the end user selects one of the users from the dropdown list, theFindProfilesByUserName()

method is used to display the profile of the selected user Again, aProfileInfoCollectionobject is

returned from this method as well

Trang 9

To delete the profile of the user selected in the DropDownList control, simply use theDeleteProfile()

method and pass in the name of the selected user as illustrated here:

ProfileManager.DeleteProfile(DropDownList1.Text.ToString())

DropDownList1.Items.Clear()

WriteDropdownList()

WriteUserOutput()

After the profile is deleted from the system, that name will not appear in the drop-down list anymore

(because the DropDownList control has been redrawn) If you look in the database, particularly at the

aspnet_Profile table, you see that the profile of the selected user is, in fact, deleted However, also notice that the user (even if the user is anonymous) is still stored in the aspnet_Users table

If you want to delete not only the profile information of the user but also delete the user from the

aspnet_Users table, you should invoke theDeleteUser()method from theMembershipclass as presented here

ProfileManager.DeleteProfile(DropDownList1.Text.ToString())

Membership.DeleteUser(DropDownList1.Text.ToString())

DropDownList1.Items.Clear()

WriteDropdownList()

WriteUserOutput()

This use of theDeleteUser()method also deletes the selected user from the aspnet_Users table You

could have also achieved the same thing by using the other constructor of theDeleteUser()method as presented here:

Membership.DeleteUser(DropDownList1.Text.ToString(), True)

DropDownList1.Items.Clear()

WriteDropdownList()

WriteUserOutput()

The second parameter used in this operation of theDeleteUser()method deletes all data related to that

user across all the tables held in theASPNETDB.mdfdatabase

Running the ProfileManager.aspx Page

When you compile and run this page, you see results similar to those shown in Figure 15-6

From this screen, you can see that this page is dealing with an anonymous user (based upon the GUID

for the username) You can also see that theIsAnonymouscolumn is indeed checked From this page, you can then delete this user’s profile information by selecting the appropriate button on the page

Summar y

The personalization capabilities provided by ASP.NET 3.5 make it incredibly easy to make your Web

applications unique for all end users, whether they are authenticated or anonymous This system enables you to store everything from basic data types provided by the NET Framework to custom types that you create This system is more versatile and extensible than using theSessionorApplicationobjects The data is stored via a couple of built-in personalization providers that ship with ASP.NET These providers

755

Trang 10

Figure 15-6

include ones that connect with either Microsoft’s SQL Server Express Edition files or Microsoft SQL

Server 2008, 2005, 2000, or 7.0

You can also use theProfileManagerclass to manage your system’s profile information This includes

the capability to monitor and delete profiles as you deem necessary

Ngày đăng: 05/07/2014, 19:20

TỪ KHÓA LIÊN QUAN