Search
More About This Website

All information is provided "AS IS" with no warranties, and confers no rights

Login
« Can HTML Keep up with other RIA technologies? | Main | RIA Services DomainDataSource.Data not updating »
Tuesday
Jul282009

RIA Services Staying Logged In

One of the more useful services that comes out of the box with .NET RIA Services is an authentication service.  In fact, if you create an application using the Silverlight Business Application template it comes already wired up for use with the ASP.NET authentication system.  Brad Abrams has a good walkthrough of the basics of using this service here.

As configured when you create your new application using this template the login is not retained from one browser session to the next.  We are in the process of building a training center application and a frequent request from users is to not have to log in each time.  They would like to be able to click “Keep me logged in” and have it be retained for a period of time.  In the rest of this blog post I’m going to walk through one of the ways to enable this using the .NET RIA Services authentication service.

The first step is to modify the LoginWindow to have a check box to allow the user to indicate they want to stay logged in as you can see in the following image.

image

This requires a simple addition to the XAML for the LoginWindow as you can see below.

<StackPanel Style="{StaticResource LoginControlStyle}">
    <TextBlock Text="" Style="{StaticResource LoginTextStyle}"/>
    <CheckBox x:Name="checkStayLoggedIn" Content="Stay logged in 2 weeks"></CheckBox>
</StackPanel>

We chose two weeks because of the type of application you can make this as long or as short as you would like.  Really what we are going to use this for is to decide if we check the persisted property on the Login call.  Previously, our login call looked like the following just passing the user and password. 

_authOp = _authService.Login(this.loginUserNameBox.Text, this.loginPasswordBox.Password);

We are going to change this logic to to set the IsPersited property on the LoginParameters as you can see in the following revised code.

LoginParameters loginParms = new LoginParameters(this.loginUserNameBox.Text,
           this.loginPasswordBox.Password, checkStayLoggedIn.IsChecked.Value, string.Empty);
_authOp = _authService.Login(loginParms);

Now because we said we are going to keep them logged in for 2 weeks, we also need to make a small change server side to the authentication section in the web.config.  Here we are going to add a Forms timeout with a value in minutes.

<authentication mode="Forms">
  <forms timeout="20160"/>
</authentication>

So at this point that all works, however, when we revisit the application it doesn’t know we are already logged in.  In order to know that, we need to add a call to the LoadUser method.  In this particular application in the MainPage loaded event handler we are showing the LoginWindow by default to allow the user to login.  Our revised code now calls the LoadUser async method from the loaded event handler as you can see below.

AuthenticationService _authService = RiaContext.Current.Authentication;

var loadUserOp = _authService.LoadUser();
loadUserOp.Completed += new EventHandler(loadUserOp_Completed);

Finally, when this completes it calls the loadUserOp_Completed event handler and we can check to see if the user is logged in and if not show the LoginWindow

void loadUserOp_Completed(object sender, EventArgs e)
{
    if (!RiaContext.Current.User.IsAuthenticated)
        new LoginWindow().Show();
}

In the case where the user had persisted the login the IsAuthenticated is now set to true and we don’t show the LoginWindow.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>