Silverlightの期限切れ終了とWCFサービスセッションの期限切れの同期スキーム.


前の文章で、WCFとAspについて話しました.Netのセッション共有は、Silverlightの期限切れの終了とWCFサービスセッションの期限切れの同期スキームを実現しました.
Silverlightのbussinessテンプレートにはタイムアウト認証の部分が含まれており、参考としてWCFサービス側sessionの失効とSilverlightの失効の同期を実現しています.
1.web.configでsessionの有効期限を設定します.
  <sessionState
                        mode="InProc" stateConnectionString= "tcpip=127.0.0.1:42424" cookieless="false" timeout="60"/>
2. aspxのページでsilverlightに構成の有効期限を読み込みます.
public partial class Default : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                string url = Request.Url.ToString();
                url = url.Replace("&", "%26");
                string urlBase64 = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(url));
                string urlRedirect = string.Format("{0}?ReturnUrl={1}", FormsAuthentication.LoginUrl, urlBase64);
                Response.Redirect(urlRedirect);
                return;
            }
            else
            {
                UserId = HttpContext.Current.User.Identity.Name; //   session    
                int formTimeOut =(int)FormsAuthentication.Timeout.TotalMinutes;
                Timeout = HttpContext.Current.Session.Timeout >= formTimeOut
                              ? formTimeOut
                              : HttpContext.Current.Session.Timeout;
            }
            base.OnLoad(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public string UserId { get; set; }

        public int Timeout { get; set; }
    }
3.Timeoutトランスミッタパラメータをsilverlight端に移動します.
<param name="initParams" value="userId=<%=UserId%>,timeOut=<%=Timeout%>"/>
4.silverlightで期限切れ時間値を読み出し、期限切れ終了実装を行う
 private void Application_Startup(object sender, StartupEventArgs e)
{
   _timeOut = Convert.ToInt32(e.InitParams["timeOut"]);
  FormsWithTimeoutAuthentication formsWithTimeoutAuthentication = new FormsWithTimeoutAuthentication(_timeOut); //      
  formsWithTimeoutAuthentication.EndLogin(true);
}

M o m m o m s w i t h t i m e outAuthenticationクラスの実装:
   public class FormsWithTimeoutAuthentication
    {
        private DispatcherTimer idleTimer;
        private int minutesIdle;
        private bool idle;
        private bool attached = false;
        private CommonProxy.CommonServicesClient commomProxy = null;
        public FormsWithTimeoutAuthentication()
            : this(20)
        { }
        public FormsWithTimeoutAuthentication(int idleMinutes)
        {
            IdleMinutesBeforeTimeout = idleMinutes;
            idleTimer = new DispatcherTimer();
            idleTimer.Interval = TimeSpan.FromMinutes(1);
            idleTimer.Tick += new EventHandler(idleTimer_Tick);
        }
        public int IdleMinutesBeforeTimeout
        {
            get;
            set;
        }
        public void  EndLogin(bool loginRes)
        {
            if (loginRes == true)
            {
                if (!attached) AttachEvents();
                minutesIdle = 0;
                idleTimer.Start();
            }
        }
        protected void  EndLogout()
        {
            idleTimer.Stop();
        }

        private void AttachEvents()
        {
            attached = true;
            //Application.Current.RootVisual.MouseMove += new MouseEventHandler(RootVisual_MouseMove);
            Application.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);
            Application.Current.RootVisual.MouseLeftButtonUp += new MouseButtonEventHandler(RootVisual_MouseLeftButtonUp);
            Application.Current.RootVisual.MouseRightButtonDown += new MouseButtonEventHandler(RootVisual_MouseRightButtonDown);
            Application.Current.RootVisual.MouseWheel += new MouseWheelEventHandler(RootVisual_MouseWheel);
        }

        void RootVisual_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            idle = false;
        }

        void RootVisual_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            idle = false;
        }

        void RootVisual_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            idle = false;
        }
        private void RootVisual_KeyDown(object sender, KeyEventArgs e)
        {
            idle = false;
        }
        private void RootVisual_MouseMove(object sender, MouseEventArgs e)
        {
            idle = false;
        }
        private void idleTimer_Tick(object sender, EventArgs e)
        {
            if (idle == true)
            {
                minutesIdle += idleTimer.Interval.Minutes;
                if (minutesIdle >= IdleMinutesBeforeTimeout)
                {
                    Logout();
                }
            }
            else
            {
                minutesIdle = 0;
            }
            idle = true;
        }
        public void Logout()
        {
            EndLogout();
            //             ,      WCF        ,    ,      
            commomProxy = new CommonServicesClient();
           
            commomProxy.GetConfigValuebyConfigKeyCompleted += (sender, e) =>
                                                                  {
                                                                      if(e.Error == null)
                                                                      {
                                                                          if (!string.IsNullOrEmpty(e.Result))
                                                                              HtmlPage.Window.Navigate(new Uri(
                                                                                                           e.Result,
                                                                                                           UriKind.
                                                                                                               Absolute));
                                                                      }
                                                                  };
            commomProxy.GetConfigValuebyConfigKeyAsync("LogoutRedirectUrl");
        }
    }