RIA Services Staying Logged In (Ria Service持久登陆,session-cookie,notcookie)

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.

原文地址:http://blog.davidyack.com/journal/2009/7/29/ria-services-staying-logged-in.html

如果不知道怎么做完整的程序,参考http://www.cnblogs.com/shenfengok/archive/2011/11/07/2239313.html

http://www.cnblogs.com/shenfengok/archive/2011/11/09/2243793.html

转载于:https://www.cnblogs.com/shenfengok/archive/2011/11/10/2244395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值