A piece of paper was thrown in front of me on table by my friend in local coffee shop, that paper was nothing but print out of one already designed ASP.NET site which now he want to convert to Silverlight with few of pages as kind of POC for Silverlight. I just saw “Welcome : <Username>” label on that, and I asked him how he is taking care of Sessions in Silverlight? and a puzzle for him..since he never implemented, there are several solutions on net by many people and enthus, here is my piece of code which I feel simple, offcourse you can try that out and help me improve if there any better way to do so.
To show this particular functionality, I am re-using my old code of Master pages in Silverlight since I need more than one page with navigation which you can find it here :
http://pendsevikram.blogspot.com/2008/11/silverlight-2-master-pages.html
Also, if you are not comfortable with Silverlight-enabled WCF services , you can see my old article :
http://pendsevikram.blogspot.com/2008/10/silverlight-2-grid-with-silverlight.html
Step 1 : Create 2 Silverlight Pages
Page.xaml :
<Grid x:Name="LayoutRoot" Background="AliceBlue">
<TextBlock Height="23" Margin="94,8,116,0" VerticalAlignment="Top" Text="Explore .NET with Vikram Pendse" TextWrapping="Wrap" Foreground="#FF000000"/>
<StackPanel x:Name="stk" Width="400" Margin="0,66,0,30">
<TextBlock Margin="78,10,0,30" Text="UserName:"/>
<TextBox Margin="40,-50,0,30" x:Name="txtuser" Height="20" Width="150" />
<TextBlock Margin="78,-5,25,30">Password :</TextBlock>
<PasswordBox Margin="40,-60,0,10" x:Name="txtpassword" Height="20" Width="150" />
<Button x:Name="btnSubmit" Height="20" Width="100" Content="Login" Click="btnSubmit_Click" />
</StackPanel>
<TextBlock Height="23" Margin="106,0,104,3" VerticalAlignment="Bottom" Text="Microsoft .NET for everyone!!" TextWrapping="Wrap" Foreground="#FF000000"/>
</Grid>
Welcome.xaml :
<Grid x:Name="LayoutRoot" Background="AliceBlue">
<StackPanel x:Name="mystk" Width="400" Margin="0,66,0,30">
<TextBlock Margin="128,-5,0,30" Text="Welcome :"/>
<TextBlock Margin="196,-46,0,30" x:Name="txtusername" />
</StackPanel>
</Grid>
Step 2 : Add a Silverlight-enabled WCF Service and access it in Silverlight application
MyService.svc.cs :
using System.Web;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
HttpContext myctxt = HttpContext.Current;
[OperationContract]
public string Login(string username, string password)
{
//Validate Username and Password against the Database.
myctxt.Session["Uname"] = username;
return username;
}
[OperationContract]
public string ReturnUser()
{
string user;
user = (string)myctxt.Session["Uname"];
return user;
}
Note : For demo purpose, I have not validated User-id and Password against database, please refer comment.
Step 3 : Consume SL-enabled WCF service in each page and pass the corresponding parameters.
First we need to send username and password from Page.xaml.cs to Service, Then Login() will maintain that particular username in session, Then we will again call the service and access that particular username which is in session by ReturnUser()
Page.xaml.cs :
private void NavigateRequest(int w)
{
if (w > 0)
{
stk.Children.Clear();
stk.Children.Add(new Welcome());
}
else
{
this.Content = new Page();
}
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
Srvc.MyServiceClient proxy = new SL_SessionMgmt.Srvc.MyServiceClient();
proxy.LoginAsync(txtuser.Text,txtpassword.Password);
NavigateRequest(1);
}
Here we are switching among the pages by NavigateRequest(), you can write Switch-case if there are multiple pages.
Welcome.xaml.cs :
public Welcome()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Welcome_Loaded);
}
void Welcome_Loaded(object sender, RoutedEventArgs e)
{
Srvc.MyServiceClient proxy = new SL_SessionMgmt.Srvc.MyServiceClient();
proxy.ReturnUserCompleted += new EventHandler<SL_SessionMgmt.Srvc.ReturnUserCompletedEventArgs>(proxy_ReturnUserCompleted);
proxy.ReturnUserAsync();
}
void proxy_ReturnUserCompleted(object sender, SL_SessionMgmt.Srvc.ReturnUserCompletedEventArgs e)
{
txtusername.Text = e.Result;
}
And here we are getting username which is in session by e.Result, This will display the username in session on Textblock on second page, it will look like this.
Here you can see whatever we pass as username, we place it in session by WCF service and get that data from session via service again and bind it to another control on another page.
Hope this will useful for you, if you are planning to implement Sessions in your Silverlight applications.
Vikram.