2015-01-30

“Muggle-born have no access to go inside the chamber of secret Harry, and only a heir of Slytherin can open the chamber. Since he is already here, the chamber of secret, has already been opened.” Today I will explains how to costomize Authentication in SilverLight.

As you have guessed already, those lines are said by Albus Dumbledore to Harry Potter. Wonder why Harry Potter? Actually, our Silverlight is very much similar to that of Hogwarts. In the case of authentication, I guess, to be precise. Silverlight provides the facility to authenticate the user using a particular query, making sure whether the querying user has the access to use the query or not. Just like you must authenticate yourself, before the door of any house in Hogwarts, while requesting it to open. If you are not authenticated then your request will be rejected. In Silverlight however, you must authenticate yourself at the very beginning of the application, through a process we call “Sign in”.

Every application in Silverlight has the sign in process. You must login before you can use the application. a Business Template of Silverlight does come with this process in a build. But herein, we will create our own custom authentication classes from scratch. So let’s jump in with both feet and create the application.

Before everything else, we need to create the database to use in the application. In SQL Server 2008 for the database that I am using for the demo, execute the following query.

This will create the Database “MyData” and a table inside it, “UserDetail”.



The image above shows my UserDetail table in the MyData database. Now let us create some users. For that, make some entries in the database.

Database is set and we are ready to go. The next step is to create a blank Silverlight application, “AuthDemo”.



When the application is created and opened, you must have greeted with the mainpage.xaml file. So, before we go any further into services etc., let’s first create a beautiful user interface for our application. Add the following lines of XAML into the grid of the main page

Our layout is also ready. It’s time now to do things that a real developer does, create models and services. Entity data models are the ADO.Net entities that are linked to the database, and work as the mediator between the application and the database. I am not going to show in detail here how to create the entity data model in the application. Rather I am assuming that readers at least know how to create the data model in Silverlight from the database. So create an entity data model basing it on the database that we just have created, MyData, and name it “AuthModel”. It’s a good practice to keep the model, services, classes and others inside separate folders in the project. So the AuthModel should be placed in the Model folder.

Build the project once.

If the project builds and compiles successfully, the time has come to add services to it. First let us add a Domain Service. Right-click on the service folder (if you have created one, else right-click on the entire project itself) in the web project, select “Add” –> “New Item” and choose “Domain service”. Name the service “UserDetailDomainService”.



After clicking on the Add button, you will see a dialog box open asking to select the entities to be included in the service. For now the box would only have User Detail. Just check the enable editing box, click “Ok” and you are done.

You are now ready with the Service and Model. The next thing to do is to add an authentication service. Just as you do to add the domain service, do exactly the same to add the authentication service, but this time now, choose “Authentication Domain Service” instead. Name the service “CustomAuthDomainService”. Remove “AuthenticationBase<User>” and remove the User class then Inherit and the “implement LinqToEntitiesDomainService<AuthEntities>” class and the “IAuthentication<UserDetail>” interface.

Before proceeding further, create another folder named “Classes” in the web project and add a class “UserDetail” to it. Add the keyword “partial” to make the class a partial class of the UserDetail class that already exists in the UserDetailMetadata that we created with the service. Now implement the IUser interface in the class. On doing so, your class would have become:

The Name property is not required. Remove that property, also since the two UserDetail classes (one of Metadata and one above) are partial, that is both the classes are incomplete without each other. Wow! How elegant. So they must be in the same namespace. Since our Metadata’s class was in the “AuthDemo.Web.Model” we need to change the namespace of this class also, from “AuthDemo.Web.Classes” to “AuthDemo.Web.Model”. Change the namespace, remove the Name property, then just compile.

Here is the final version of the UserDetail Class:

Now remove the exception throw and add the following code into each function as done here:

Moving back to our CustomAuthentication Service that now looks like:

Now remove the exception throw and add the following code into each function as done here:

We are getting very close.

In the constructor of App.xaml.cs add the following line to set the WebContext as the global object throughout the application:

Ok, all over. Build and run the project.

Best Recommended SilverLight Hosting

ASPHostPortal.com

ASPHostPortal.com is the leading provider of Windows hosting and affordable Silverlight Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.

HostForLIFE.eu

HostForLIFE.eu guarantees 99.9% uptime for their professional SilverLight hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. HostForLIFE.eu has supported SilverLight,  Relibility, Stability and Performance of  servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.

DiscountService.com.au

DiscountService.com.au is The Best and Cheap SilverLight Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you.

Show more