博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RIA Services Staying Logged In (Ria Service持久登陆,session-cookie,notcookie)
阅读量:6155 次
发布时间:2019-06-21

本文共 3473 字,大约阅读时间需要 11 分钟。

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 .

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.

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.

原文地址:

如果不知道怎么做完整的程序,参考

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

你可能感兴趣的文章
JavaAPI详解系列(1):String类(1)
查看>>
HTML条件注释判断IE<!--[if IE]><!--[if lt IE 9]>
查看>>
发布和逸出-构造过程中使this引用逸出
查看>>
使用SanLock建立简单的HA服务
查看>>
Subversion使用Redmine帐户验证简单应用、高级应用以及优化
查看>>
Javascript Ajax 异步请求
查看>>
DBCP连接池
查看>>
cannot run programing "db2"
查看>>
mysql做主从relay-log问题
查看>>
Docker镜像与容器命令
查看>>
批量删除oracle中以相同类型字母开头的表
查看>>
Java基础学习总结(4)——对象转型
查看>>
BZOJ3239Discrete Logging——BSGS
查看>>
SpringMVC权限管理
查看>>
spring 整合 redis 配置
查看>>
cacti分组发飞信模块开发
查看>>
浅析LUA中游戏脚本语言之魔兽世界
查看>>
飞翔的秘密
查看>>
Red Hat 安装源包出错 Package xxx.rpm is not signed
查看>>
编译安装mysql-5.6.16.tar.gz
查看>>