Authentication and Profile Services in asp.net Ajax
With .net 3.5 Microsoft introduced authentication and profile services which allows you to authenticate using client side libraries. You also have the ability to update values in your profile using asp.net profile services. To work with this example, you need to set to up Membership ship database by using aspnet_regsql command. Enable form based authentication by changing the authentication mode to Forms. If you have created asp.net 3.5 website project, you will see profile and authentication and role service registered in config section of web.config as follows.
Among the many services that are registered you can see that asp.net Ajax comes with additional web services that allows you to authenticate with the asp.net Membership database. These sections are simply registered but are not ready for use. In order to these services from client side code, you need to turn them on by adding the following JavaScript.
In the above code, I am enabling two web services that will be available to the client using asp.net Ajax. I am turning on profile service and also telling which properties would be available for read and write from the asp.net Ajax client side. Next I am enabling authentication service to authenticate client using JavaScript and webserice.
My profile basically contains two properties, background color and Display Name which I have configured in my web.config file as follows.
I created a page with two textboxes for username and password and button that has a JavaScript function registered to it called Login. Here is the html that builds the UI to login.
I have a login button button which simply calls the JavaScript function called login that is responsible for checking if the user has a valid login
In the login method, I am calling login method on the authentication service passing in the callback for LoginComplete and AuthFailed methods. LoginComplete will always be called regardless if login failed or succeeded. You have to use the parameter credentials to determine if the login failed or succeeded and change your UI respectively. OnAuthFailed method will be called by the authentication service only when asp.net Ajax service will not be able to communicate with the service on the server. If the login succeeds, I update my UI by calling Refresh method. After reaching in the Refresh method, I am sure the user is authenticated, so I call the load method on the profile to retrieve the profile information of the logged in user. The load method takes in a callback to the successful and failed event after making attempt to retrieve the profile information for the logged in user. Inside the OnLoad event I retrieve the properties from the profile and assign it to the textboxes on my UI.
On the same page I am also have a save Profile button which allows the logged in user to save their profile information using the profile service to Membership database. Here is the markup that accomplishes that.
In the SaveToProfile method, I am simply grabbing the values from the textboxes and assigning it to the properties exposed by the profile service. After assigning the value I call save on the profile service to save the profile information to the membership database.
Using the authentication service, you also have the ability to logout. Authentication service exposes a method called logout. Here is the function that will logout the user from client side.