Consuming Membership and Profile Services via ASP.NET AJAX
ASP.NET 2.0 introduced various application services—such as Membership, Roles, and Profiles—that eliminate a lot of coding that was required to provide the same functionality. However, these services are part of ASP.NET's server-side framework, which could pose a challenge when you use ASP.NET AJAX to consume the services from client-side JavaScript code. Fortunately, ASP.NET AJAX provides an out-of-the-box solution to this problem. This article explains how to use this solution in C# with Visual Studio.
Sample Scenario
Figure 1. Creating a New ASP.NET AJAX-enabled Web Site
Configuring the Web Site
Before you begin coding, configure the web site for forms authentication as well as Membership and Profile services. Open a web.config file in your Visual Studio IDE and add the following markup inside the connectionStrings section:
<connectionStrings>
<add name="connstr"
connectionString="data source=.\sqlexpress;
initial catalog=northwind;
integrated security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
You specified a database connection string named connstr that points to a Northwind database. Make sure to change the connection string to match your development environment. I assume that your database is configured for application services using the aspnet_regsql.exe tool. You will use this connection string while configuring membership and profile providers.
Now, add the following markup inside the system.web section:
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<membership defaultProvider="p1">
<providers>
<add name="p1"
connectionStringName="connstr"
type="System.Web.Security.SqlMembershipProvider"
requiresQuestionAndAnswer="false"/>
</providers>
</membership>
<profile defaultProvider="p2">
<providers>
<add name="p2"
connectionStringName="connstr"
type="System.Web.Profile.SqlProfileProvider"/>
</providers>
<properties>
<add name="FullName"/>
<add name="DOB" type="System.DateTime"/>
<group name="Address">
<add name="Street"/>
<add name="Country"/>
<add name="PostalCode"/>
</group>
</properties>
</profile>
Review the above markup carefully, and you'll notice the following:
- The authentication section sets the authentication mode to Forms. The forms tag sets the URL of the login page by using the loginUrl attribute.
- The authorization section disables anonymous users by setting the users attribute of the deny tag to "?".
- The membership section configures a membership provider named p1. (You can change this any name you choose.)
- The connectionStringName attribute specifies the database that will be used for storing membership information.
- The type attribute indicates the class that will act as the membership provider. You use the built-in SQL Membership provider called SqlMembershipProvider.
- The requiresQuestionAndAnswer attribute indicates that you do not intend to accept a secret question and answer from the end user at the time of registration.
- The profile section configures a profile provider named p2 and various profile properties. The significance of the connectionStringname and type attributes is same as for the membership section. Note, however, that this time the type is a SqlProfileProvider class. The properties section defines profile properties and groups.
- You defined two simple properties called FullName and DOB and a property group called Address. The Address group further contains three properties: street, country, and postalcode. The DOB property is of type DateTime; therefore, its type attribute is set to System.DateTime.
Now that you have configured your web site for using forms authentication and membership services, it's time to expose Membership and Profile services to the client-side AJAX code. The web.config file will have a pre-defined section called webServices. By default, all its content is commented. You need to un-comment and modify it so that it looks as shown below:
<webServices>
<authenticationService enabled="true"
requireSSL="false"/>
<profileService enabled="true"
readAccessProperties="FullName,DOB,Address.Street,Address.Country,
Address.PostalCode"
writeAccessProperties="FullName,DOB,Address.Street,Address.Country,
Address.PostalCode"/>
</webServices>
The authenticationService tag is used to expose forms authentication and membership services to AJAX code. The enabled attribute governs whether AJAX code can avail membership services. The requireSSL attribute indicates whether the authentication is happening over SSL. Similarly, the Profile service is exposed to AJAX code by using the profileService tag. The readAccessProperties and writeAccessProperties attributes of the profileService tag specify the profile properties that are readable and writable, respectively. Notice how the grouped properties are specified using the dot (.) notion. If you do not include a specific profile property in these attributes, it will not be accessible to the client code.
Applying forms authentication ensures that all the forms of the web site except the login page are secured. However, you want your registration page to be unsecured because new users will need to access it. Do this by adding a location section in the web.config file as shown below:
<location path="register.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
The path attribute of the location tag specifies a virtual path of a file or folder that is to be configured. It then allows access to all the users using the authorization section and allow tag.
This completes the web site configuration. Now, you will move on to develop the required web forms.
User Registration
First of all, you will create the user registration page. Add a new web form named Registration.aspx. Drag and drop a ScriptManager control from the toolbox (see Figure 2).
Figure 2. Drag and Drop a ScriptManager Control
Add a table into the UpdatePanel and design it as shown in Figure 3.
Figure 3. Design for Table in the UpdatePanel
[WebMethod]
public static bool CheckAvailability(string uid)
{
MembershipUser user = Membership.GetUser(uid);
if (user == null)
{
return true;
}
else
{
return false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MembershipUser user = Membership.CreateUser
(TextBox2.Text, TextBox3.Text, TextBox5.Text);
ProfileCommon pc = Profile.GetProfile(user.UserName);
pc.FullName = TextBox1.Text;
pc.DOB = DateTime.Parse(TextBox6.Text);
pc.Address.Street = TextBox7.Text;
pc.Address.Country = TextBox8.Text;
pc.Address.PostalCode = TextBox9.Text;
pc.Save();
lblMsg.Text = "User created successfully!";
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
}
function CheckAvailability()
{
var uid=document.getElementById('TextBox2').value;
if(uid=="")
{
alert('Please enter user ID!');
return false;
}
PageMethods.CheckAvailability(uid,OnComplete);
return false;
}
function OnComplete(result)
{
var lblMsg=document.getElementById('lblMsg');
if(result)
{
lblMsg.innerText="The ID is available!";
}
else
{
lblMsg.innerText="The ID is unavailable!";
}
}
Figure 4. Sample Run of the Web Form
Developing a Login Page
Now that users can register themselves with the web site, you need to provide a facility that enables them to log in and access various pages. To do so, add a new web form called Login.aspx to the web site. Remember that you have set the loginUrl attribute of the forms tag to Login.aspx. Drag and drop a ScriptManager control on it and design the login page as shown in Figure 5 by assembling various controls.
Figure 5. The Login Page Design
As you can see, the login page consists of textboxes for entering a user ID and password. The "Remember Me" checkbox allows you to preserve your logged-in status even after closing the browser window. The TextMode property of the password textbox is set to Password. Further, the OnClientClick property of the Login button is set to "return BeginAuthenticateUser();". BeginAuthenticateUser() is a JavaScript function that uses the ASP.NET AJAX authentication service to authenticate the user. The following is the BeginAuthenticateUser() function:
function BeginAuthenticateUser()
{
var uid;
var pwd;
var isPersistent;
uid=document.getElementById('TextBox1').value;
pwd=document.getElementById('TextBox2').value;
isPersistent=document.getElementById('CheckBox1').checked;
Sys.Services.AuthenticationService.login
(uid,pwd,isPersistent,null,null,
EndAuthenticateUser,OnError,uid);
return false;
}
The BeginAuthenticateUser() JavaScript function retrieves the user IDs and passwords entered in their respective textboxes. It also retrieves the status of the "Remember Me" checkbox. ASP.NET AJAX provides a built-n class called AuthenticationService that resides in the Sys.Services namespace. Remember that the Sys.Services namespace is defined by the client-side framework of ASP.NET AJAX. The AuthenticationService class offers two methods: login() and logout(). The code above used the login() method, which takes in all eight parameters. Their significance is listed below:
Parameter
Significance
1
A user ID
2
A password
3
A boolean value indicating whether an authentication cookie will be persistent
4
The web page where the user should be redirect after a successful login
5
Reserved for future use
6
A callback function that will be called after a successful login (EndAuthenticateUser in this example)
7
A callback function that will be called in case a login attempt fails (OnError in this example)
8
A custom value that is passed to the callback functions
If the user is successfully authenticated, the EndAuthenticateUser function will be called. The following is the EndAuthenticateUser function:
function EndAuthenticateUser(result,userContext,methodName)
{
if(result)
{
window.location='default.aspx';
}
else
{
alert("Unable to login! Please check user id and password!!");
}
}
The EndAuthenticateUser() function takes three parameters: the result of the login operation, the user context that you passed earlier in the eighth parameter of the login() method, and the method name. Inside, it checks whether the result is true (in other words, the user is successfully authenticated) and, if so, it sets the location property of the windows object to default.aspx. This way, the user is redirected to the default page after a successful login attempt. If there is any error, an error message is displayed using the alert() function.
The OnError() function is called whenever an error occurs when calling the authentication service. This function is shown below:
function OnError(result,userContext,methodName)
{
alert(result.get_message());
}
The function simply displays an error message to the user. The result parameter received is actually an object and has a method called get_message() that returns a descriptive error message.
This completes the login page.
---
Implementing Logout Functionality
Add another web form called Default.aspx. This web form will allow users to logout and manage their profiles. Firstly, you will implement logout functionality. Drag and drop a ScriptManager control on the Default.aspx and design the web form as shown in Figure 6.
protected void Page_Load(object sender, EventArgs e)
{
Label4.Text = Membership.GetUser().UserName;
}
function BeginLogOut()
{
Sys.Services.AuthenticationService.logout
(null,EndLogout,OnError,null);
return false;
}
function EndLogout(result)
{
//nothing here
}
- The first parameter indicates a URL where the user should be taken after successful logout.
- The second parameter specifies a callback function to be called when the logout operation is complete.
- The third parameter specifies a callback function to be called when the logout operation fails.
- The fourth parameter indicates a custom context information.
Reading Profile Properties
function pageLoad()
{
var panel3=document.getElementById('Panel3');
panel3.style.visibility="hidden";
}
function BeginProfileLoad()
{
if(event.srcElement.value=="Show my profile")
{
var panel3=document.getElementById('Panel3');
panel3.style.visibility="visible";
event.srcElement.value="Hide my profile";
Sys.Services.ProfileService.load
(null,EndProfileLoad,OnProfileFailed, null);
}
else
{
var panel3=document.getElementById('Panel3');
panel3.style.visibility="hidden";
event.srcElement.value="Show my profile";
}
return false;
}
Parameter
Significance
1
An array of property names that are to be loaded. If you have too many profile properties, then it makes sense to load the ones that you really want to use. This will improve the performance of your page.
2
A callback function that will be called when the load operation is completed
3
A callback function that will be called if the load operation fails
4
Custom context information, if any
Once the profile is loaded the EndProfileLoad() function is called. This is the EndProfileLoad():
function EndProfileLoad(numProperties, userContext, methodName)
{
document.getElementById('TextBox3').value =
Sys.Services.ProfileService.properties.FullName;
document.getElementById('TextBox4').value =
Sys.Services.ProfileService.properties.DOB;
document.getElementById('TextBox5').value =
Sys.Services.ProfileService.properties.Address.Street;
document.getElementById('TextBox6').value =
Sys.Services.ProfileService.properties.Address.Country;
document.getElementById('TextBox7').value =
Sys.Services.ProfileService.properties.Address.PostalCode;
}
Modifying Profile Properties
function BeginSaveProfile()
{
Sys.Services.ProfileService.properties.FullName=
document.getElementById('TextBox3').value;
Sys.Services.ProfileService.properties.DOB=
document.getElementById('TextBox4').value;
Sys.Services.ProfileService.properties.Address.Street=
document.getElementById('TextBox5').value;
Sys.Services.ProfileService.properties.Address.Country=
document.getElementById('TextBox6').value;
Sys.Services.ProfileService.properties.Address.PostalCode=
document.getElementById('TextBox7').value;
Sys.Services.ProfileService.save
(null,EndSaveProfile,OnProfileFailed,null);
return false;
}
function EndSaveProfile(numProperties, userContext, methodName)
{
alert('Your profile is saved successfully!');
}
Figure 7. A Sample Run of the Default.aspx
No comments:
Post a Comment