.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Custom Authentication In Silverlight4 Using RIA Service

Posted By:Manas Patnaik       Posted Date: March 21, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In my previous article we had discussed about Data Binding ,Validation,Exception Handling ,Handling Multiple table and many more.But the first step in a business application deals with Authentication.Surely nobody wants data to be meshed up by an anonymous user so in this article we are going to implement Authentication to SOI (Satate of India) application.This post is restricted to FormsAuthentication model only.
 

In my previous article we had discussed about Data Binding ,Validation,Exception Handling ,Handling Multiple table and many more.But the first step in a business application deals with Authentication.Surely nobody wants data to be meshed up by an anonymous user so in this article we are going to implement Authentication to SOI (Satate of India) application.This post is restricted to FormsAuthentication model only.Also i am going to skip Authorization and due for next article.

Well authentication in Silverlight 4 using RIA service in business application can be implemented by 2 ways

  • Using Default authentication provided by Silverlight Business Template (Using ASP.Net Role and Membership)
  • Using Custom Authentication (Using your own database and  model )

You can find number of post/articles available in blogs that's deals with default authentication with Asp.Net membership provider .If you want to find know more about  this MSDN article worth a look .

SO lets start with Authentication using your own custom table for application.

Scenario

This article going to demonstrate the steps involved for integrating  authentication to States of India (SOI App) using a custom table which will store basic information such as UserName,Password and Email.Before proceeding with SOI app lets have a look into the app with my previous articles.

Custom Authentication  using RIA Services

So here the application aim to have following functionality

  1. The User have access to the Home page , where they can view the State and its information
  2. But Adding a new state or Editing state requires user to be authenticated

Steps Involved For Implementing Authentication

Before going into detail of each steps the image bellow shows a overall picture/steps.Follow the Arrow mark for sequence , as there is a human tendency of reading Left to Right Smile.

Custom Authentication  using RIA Services

Adding a new Table to Database and Updating the Data model

The first step is to add a table to application database , here i have added a new table called UserDetail with following fields.

Custom Authentication  using RIA Services

Then go to your Model at server side project and update the Model.In case you need the detail of setting up a Entity Data Model then refer to my earlier post Here.

Custom Authentication  using RIA Services

Select the newly added UserDetail table and proceed.Along with the newly added entity to data model it will be as bellow.If you notice than you can mark that the UserName column in the  table is mapped as Name to UserDetail entity , we will discuss about it later in this post.

Custom Authentication  using RIA Services

The Basic Server Side Setup for Custom Authentication

CHANGES TO WEBCONFIG FILE

As we opted for Forms Authentication, the very first change we need is to add the authentication in webconfig file.So add following section to the webconfig file at server side project.

  1. <authentication mode="Forms">
  2. authentication>
THE CONCEPT

Before i proceed with rest of the article lets have a look into some concept and how it will work.When you use a business template in Silverlight ,visual Studio adds Authentication service derived from AuthenticationBase and User Derived form UserBase.The user information and roles are stored with default ASP.Net membership provider table ASPNETDB.

But we are going to use UserDetail table instead of default ASPNETDB and our own Domainservice to handle authentication.Here comes theIAuthentication and Iuser interfaces.

Custom Authentication  using RIA Services

So our Custom DomainService is going to be implement IAuthentication , where T is the type of UserDetail,  in turns the interface will make available the UserDetail to both client and server side.

As the UserDetail entity is going to be propagated to client side using IAuthentication it requires to implement IUser interface.The update of Entity Data Model add UserDetail entity to model and generates  following piece of code along side State and City entity.

Custom Authentication  using RIA Services

Our Database Table does not holds  Name property as its required from IUser interface point of view  ,so we need to map UserName to Name in UserDetail entity.You can avoid this step If your table has a Name column .For the time being here i am going to skip Role as implemented by IUser.

Custom Authentication  using RIA Services

Then lets add a class named UserDetail to implement IUSer interface,Custom Authentication  using RIA Services

  1. public partial class UserDetail : IUser
  2. {
  3. #region IUser Members
  4. [DataMember]
  5. public IEnumerable<string> Roles
  6. {
  7. get
  8. {
  9. return null;
  10. }
  11. set
  12. {
  13. throw new NotImplementedException();
  14. }
  15. }
  16. #endregion
  17. }
  18. }

So partial classes for UserDetail at EntityModel and above UserDetail sets the valid User which is going to be use  in the Custom Authentication domain service ,which we are going to add in  next Step.

ADDING A DOMAINSERVICE FOR AUTHENTICATION

Lets add a new DomainService named SOIAuthDomainService , with out selecting any entity .In the Domain service implement IAuthenticationinterface

Custom Authentication  using RIA Services

Here in SOIAuthDomainService along with the default methods we will add some more method for Validation and insertion of a user.And also implement logic for Login,Logout .The DefaultUser is  the user we are going to return in case the login failed.So overall the class structure will be as bellow

Custom Authentication  using RIA Services

The Login Logic in the domain service as follows

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend