Silverlight 4 Business Application Part 1 of n

Logins, Users, Roles, Page Content

Currently I am trying to learn Silverlight Business Application development, and during this phase I will try to post some interesting tips and tricks on which I will encounter. I hope you will find some of a code usefull.

In this post I will show how to create starting silverlight business application skeleton, and how you can manipulate with users, roles and content within Silverlight application. As you already know, Silverlight BA uses ASP.NET database model in order to display or hide business data via RIA Services. So, if you want to develop SL application with several modules and each module can access different set of users, you need to handle with users and rolls similar as in ASP.NET application. In this post we will make demo with three pages. Each page will be opened with different users, and admin can open all pages. The screenshot below shows the SL application.

So lets start VS 2010, and choose File New Project, and in list choose Silverlight Business Application see picture below:

Name the new project SLBAPart1 and choose OK button. The VS designer will do a lot of works for us in order to generate starter business application. VS designer generated the two page, completed login and user registration logic as well as database and RIA services. We will add another page called Product, and implementi another link button in mainpage.xaml (see picture below).

  1. To create the new Product.xaml page, right click on Views folder, and choose Add new item
  2. In opened dialog choose Silverlight Page name it Product and click OK.
  3. Add xaml code as depicted on the picture above.

When you run the application you can see the application similar as the first picture shows, which you can access all three pages. There is also “login” link button wich you can login as well as register a new user for SL application.

To define user membership in SL application, select SLBAPart1.Web project  and choose menu ASP.NET Configuration from the Project menu of Visual Studio 2010. Internet Explorer page shows content similar like picture below. Choose Security link button, and click on Creates and Manages roles. Define four roles like picture shows below.

Click Back button and create four users with corresponding roles. The final result of user defining is shown on picture below.

Each user has its own corresponding roles. When you use the attached sample pasword for each user is defined similar as for the first user “$user1user1“. Password for admin user is “$adminadmin“.

After we defined roles and users it is time to define content for each page. Open Product.xaml page from Visual Vtudio project, define content like picture below.

We have to define Public.xaml page which can be accessed if we not logged in. Similar to previous page creation define Public.xaml page, and put some text in it.

Next step in implementation is  prevent user to see content if it is not authorized. To implement that, we need to expand ContentFrame_Navigated event of the mainpage.xaml.cs.

private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
     //Check to see if user has right to navigate
     if (!UserCanNavigate(e))

     foreach (UIElement child in LinksStackPanel.Children)
         HyperlinkButton hb = child as HyperlinkButton;
         if (hb != null && hb.NavigateUri != null)
            if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                VisualStateManager.GoToState(hb, "ActiveLink", true);
               VisualStateManager.GoToState(hb, "InactiveLink", true);

When the user click for content ContentFrame_Navigated event fires up. Then UserCanNavigate method gets call. The implementation of this method is the following:

private bool UserCanNavigate(NavigationEventArgs sender)
    //if user is not authenticated show public content
    if (!WebContext.Current.User.IsAuthenticated)
        ContentFrame.Source = new System.Uri("/Public", UriKind.RelativeOrAbsolute);
        return false;
    } //If the user is loged show only content on which is authorizated
        //If user is admin alow everithing
            return true;
        //if user is in rol1 open Home content
        if (WebContext.Current.User.IsInRole("Rol1"))
            ContentFrame.Source = new System.Uri("/Home", UriKind.RelativeOrAbsolute);
            return false;
        }//Similar to previous
        if (WebContext.Current.User.IsInRole("Rol2"))
            ContentFrame.Source = new System.Uri("/About", UriKind.RelativeOrAbsolute);
            return false;
            ContentFrame.Source = new System.Uri("/Product", UriKind.RelativeOrAbsolute);
            return false;


Build application and run. The picture below shows our application. If we try to navigate to any content before we LogedIn we always get the same public content. Depending on user information when we logged in ,we get coresponding content.

Thats all for now. We learned how we can manipulate with content and different user roles. Source code for this demo can be downloaded from here.


17 thoughts on “Silverlight 4 Business Application Part 1 of n

  1. Pingback: Silverlight 4 Business Application Part 2 od n | Bahrudin Hrnjica Blog

  2. This is a great learning tutorial. Please add to it more info on getting the membership database setup using aspnet_regsql tool. That missing part cost me a full days work/research.

  3. Great tutorial. But unfortunately the code for this and the other parts are no longer available…

  4. As well as a good pair of trousers you should also consider a bib and braces, gloves, boots, eye and ear protection. When storing the chainsaw,
    keep it out oof the reach of children, and make sure that
    the blade is away from anything that could gget caught oon it.
    There are people that take their wok to festivals and faies and sell teir work to the public.

  5. I’m a plugin designer for wordpress blogs. I’ve produced a plugin that
    will help scrape web surfer’s contact information on your database without needing their interaction and communication.
    Now I am looking to find ‘beta’ test candidates and also, since you are getting substantial numbers of page views, I
    am interested in you and the blog site. Do you think you’re
    still interested?

  6. This is very attention-grabbing, You’re an excessively skilled
    blogger. I have joined your feed and look ahead to in search of extra of your magnificent post.
    Additionally, I have shared your website in my social networks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s