Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Wrox Professional Web Parts and Custom Controls with ASP.NET 2.0 phần 6 potx
Nội dung xem thử
Mô tả chi tiết
The LicenseProvider is passed a number of parameters:
❑ context: This object provides information about the environment that the control is executing in.
The UsageMode property in this object, for instance, allows you to check whether the control is
in design or run time mode.
❑ type: The datatype of the control.
❑ instance: A reference to the control.
❑ allowExceptions: When set to True, it indicates that a LicenseException is to be thrown if
licensing fails.
Now that your license and license provider are built, you need to attach the license provider to your
custom control. Follow these steps:
1. Add the LicenseProviderAttribute to your class declaration and pass the attribute a reference to
your license provider. This Visual Basic 2005 example uses the license provider created in the
previous section:
<LicenseProvider(GetType(MyLicensingProvider)), _
ToolboxData(“<{0}:MyControl1 runat=server></{0}:MyControl1>”)> _
Public Class MyControl
In C#:
[LicenseProvider(typeof(MyLicensingProvider))]
[ToolboxData(“<{0}:MyControl1 runat=server></{0}:MyControl1>”)]
public class MyControl
{
2. In your control’s constructor, your control should call the Validate method of the LicenseManager
object (the LicenseManager automatically references the LicenseProvider referenced by the
attribute on the class). If the licensing fails, a LicenseException error is raised, terminating the
control’s processing. This Visual Basic 2005 code demonstrates how the Validate method is used:
Dim lic As System.ComponentModel.License
Public Sub New()
lic = LicenseManager.Validate(GetType(MyControl), Me)
End Sub
In C#:
public class MyControl
{
System.ComponentModel.License lic;
public MyControl()
{
lic = LicenseManager.Validate(typeof(MyControl), this);
}
}
198
Chapter 7
12_57860x ch07.qxd 10/4/05 9:25 PM Page 198
3. In the Dispose method of your control, call the Dispose method of any license that you retrieved
in your control’s constructor and haven’t already disposed:
Public Overloads Overrides Sub Dispose()
If lic IsNot Nothing Then
lic.Dispose()
lic = Nothing
End If
End Sub
In C#:
public override void Dispose()
{
if(lic != null)
{
lic.Dispose();
lic = null;
}
}
In Step 2, instead of using the Validate method, you can call the IsValid method, passing the type of the
custom control. The IsValid method doesn’t throw an exception but returns False if licensing fails. This is
a Visual Basic 2005 example of the IsValid property:
Public Sub New()
If LicenseManager.IsValid(GetType(CustomControl1)) = False Then
Throw New System.Exception(“Licensing failed.”)
End If
End Sub
In C#:
public MyControl()
{
if(LicenseManager.IsValid(GetType(CustomControl1)) == false)
{
throw new System.Exception(“Licensing failed.”);
}
}
The LicenseManager object that you call from your control is created for you automatically, and handles
calling the GetLicense method of your License provider either through the LicenseManager’s Validate or
IsValid method.
This is just the bare bones of implementing licensing. You can have your License object check the licensing
source (a text file, a Web Service) for any set of conditions. You might specify usage counts in your
licensing, or list which functions on the control are to be enabled. In your license provider, you can add
additional methods or properties that interact with the license object to check for these conditions. Finally,
in your custom control, you can call these methods and properties to determine what your control is
allowed to do.
199
Developer Tools
12_57860x ch07.qxd 10/4/05 9:25 PM Page 199
Rather than writing your own licensing provider, you can use the LicFileLicenseProvider, which is part
of the .NET framework. You can create a licensing provider by inheriting from LicFileLicenseProvider
and implementing only the functions that you want to override. If you have worked with licensing in
COM applications, you’ll find that the LicFileLicenseProvider supports the same functionality as
ActiveX licensing.
Managing the Personalization Subsystem
When you deploy your Web Parts they will take advantage of the membership subsystem that is part of
ASP.NET. The membership subsystem is automatically activated and a membership data store is set up
in SQL Server Express the first time you add a WebPartManager to a Web page in Visual Studio 2005.
However, to take full advantage of personalization, you need to provide a method for users to identify
themselves so that the personalizations that users make are applied to them.
In addition, ASP.NET provides a mechanism for you to control where your personalization data is stored
and allows you to switch between data stores from within your code. As you’ll see, there are many
aspects of personalization that you can control from your code. You can even set up your page so that
changes made by one user (the system administrator, for instance) are applied to all users.
This all boils down to these three topics:
❑ Allowing users to identify themselves to the application
❑ Setting up personalization providers
❑ Managing personalization for your page
Identifying the User
ASP.NET 2.0 comes with a set of login controls that you can add to a Web page to create a login page.
The key component is the Login control, which adds text boxes for entering the username and password,
a button for logging in, and a checkbox for setting up a cookie that will let the user be remembered by
the login process. Using these controls allows a user to log on to the site with a specific identity and be
assigned the customizations that he has made.
In order for users to log on to the membership system, you need to set up those user identities for the
site. You can add new users to your site from Visual Studio 2005 with these simple steps:
1. Select ASP.NET Configuration from the Website menu.
2. When the Web site administration page is displayed (see Figure 7-11), click the Security tab.
3. Click the Create user link to display the Create User form.
If your Web site is using Windows authentication, you won’t be able to create users and the Create
User link won’t appear. To stop using Windows authentication, click the Select authentication type
link (see Figure 7-12), and then select the From the Internet option and click Done.
4. Enter the information required by the form and click the Create User button to create the user.
200
Chapter 7
12_57860x ch07.qxd 10/4/05 9:25 PM Page 200
Figure 7-11
Figure 7-12
201
Developer Tools
12_57860x ch07.qxd 10/4/05 9:25 PM Page 201
In most cases, you probably already have a list of users with usernames and passwords stored in some
datastore (such as a database or Active Directory repository). In these situations, it makes more sense to
create code that will read users from your datastore and add them to your site’s membership system.
To create a user from your code, you can use the CreateUser method of the Membership object. This
method can accept a number of parameters that set the user’s name, password, e-mail address, and
other items required on the Create User page of the administration pages. Some of the CreateUser
methods accept a MembershipCreateStatus object while others do not. If you use one of the methods
that doesn’t accept a MembershipCreateStatus object, if the user isn’t successfully created, a
MembershipCreateUserException error is raised. On the other hand, if you use one of the methods
that does accept a MembershipCreateStatus object, no error is thrown if the user isn’t successfully
created. Instead, the MembershipCreateStatus object passed as a parameter is updated with the result
from attempting to create the user.
The following Visual Basic 2005 code uses one of the versions of the CreateUser methods that accepts a
MembershipCreateStatus object. After calling the CreateUser method, the code tests the MembershipCreateStatus’s value using one of the MembershipCreateStatus enumerated values to see if the attempt
to create a user succeeded.
Dim nu As System.Web.Security.MembershipUser
Dim stat As System.Web.Security.MembershipCreateStatus
nu = System.Web.Security.Membership.CreateUser(“PeterVogel”, “CMingus”, _
“[email protected]”, “Who is a bassist”, “Charlie”, True, stat)
If stat <> MembershipCreateStatus.Success Then
Me.txtError.Text = “User creation failed.”
End If
In C#:
System.Web.Security.MembershipUser nu;
System.Web.Security.MembershipCreateStatus stat;
nu = System.Web.Security.Membership.CreateUser(“PeterVogel”, “CMingus”,
“[email protected]”, “Who is a bassist”, “Charlie”, true, out stat);
if (stat != MembershipCreateStatus.Success){
this.txtError.Text = “User creation failed.”;
}
While administering your users from Visual Studio 2005 makes sense for your test site, you will want to
use a more sophisticated tool on your production Web site. You can:
❑ Add the ASP.NET user creation controls to your pages to give you (or your users) the ability to
create users.
❑ Administer users with the Web administration pages for your Web site by selecting ASP.NET
Configuration from the Website menu from within Visual Studio 2005. From outside Visual
Studio 2005 or on a deployed site, use http:/hostname/sitename/WebAdmin.axd.
❑ Build user management pages with the Membership objects.
202
Chapter 7
12_57860x ch07.qxd 10/4/05 9:25 PM Page 202