• RIA Services Staying Logged In (Ria Service持久登陆,sessioncookie,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

  • 相关阅读:
    常见数据库设计
    团队建设工具
    Java的redis控制台-Jedis
    java常用类总结
    sql可重复执行语句例子
    让simplejson支持datetime类型的序列化
    Python模块的导入以及软件开发规范
    boto3--通过Python的SDK连接aws
    HTML目录:
    Python目录:
  • 原文地址:https://www.cnblogs.com/shenfengok/p/2244395.html
Copyright © 2020-2023  润新知