Click here to Skip to main content
15,881,709 members
Articles / Web Development / ASP.NET

Change Silverlight Business Application template to handle login state events

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Feb 2011CPOL1 min read 13.9K   4  
Change Silverlight Business Application template to handle login state events

If you have created a Silverlight Application in Visual Studio 2010, you have probably used the Business Application template. The template already has implementation to wire the login and registration tasks for the application. The login state information however is encapsulated in the LoginStatus control, and the application does not have any wiring to handle those events. The MainPage needs to handle those events to enable certain user interface elements that should only be available after the user has logged in successfully. The MainPage acts as the master page (ASP.NET) for the application.

To enable an application to received these events, we need to make the following changes:

Edit the LoginStatus.xaml.cs file and add the following code segment in the LoginStatus class:

C#
public delegate void LoginHandler(object sender, AuthenticationEventArgs e);
public event LoginHandler LoginStateChange;         //event to send when status changes

Look for the authentication event handlers and add the LoginStateChange call. The code should look as follows:

C#
private void Authentication_LoggedIn(object sender, AuthenticationEventArgs e)
{
      this.UpdateLoginState();
      LoginStateChange(this, e);  //send event
}
C#
private void Authentication_LoggedOut(object sender, AuthenticationEventArgs e)
{
      this.UpdateLoginState();            
      LoginStateChange(this, e); //send event
}

Now edit the MainPage.xaml.cs file and in the constructor add the LoginStateChange handler. The code should look as follows:

C#
public MainPage()
{
       InitializeComponent();
       LoginStatus ctrl = new LoginStatus();
       ctrl.LoginStateChange += new LoginStatus.LoginHandler(LoginStateChange);
       this.loginContainer.Child = ctrl;                         
}

Add the event handler and check for the user status to make an element visible. In this example, I am using a container named menu which I added to the MainPage.xaml file. Replace the container name with something you have previously defined in your file, otherwise you will get a compilation error.

C#
void LoginStateChange(object sender, AuthenticationEventArgs e)
{
     menu.Visibility = (e != null && e.User.Identity.IsAuthenticated) ? 
                        System.Windows.Visibility.Visible : 
                     System.Windows.Visibility.Collapsed;            
}

I hope it helps.

This article was originally posted at http://ozkary.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect OG-BITechnologies
United States United States
Software engineer, author & speaker who enjoys mentoring, learning, speaking and sharing with others about software development technologies. Microsoft MVP.

My Blog

Comments and Discussions

 
-- There are no messages in this forum --