Silverlight逾時退出怎麼實現?(AuthticationTimeOut)

來源:互聯網
上載者:User

Silverlight使用者用表單身份認證FormsAuthentication登入以後,如果一定的時間沒有動作(Idle)就逾時退出登入,這樣的功能怎麼實現?(關於Silverlight FormsAuthentication使用者登入,可以參考BusinessApplication,建立一個BusinessApplication即可)。好了,來實現這個逾時退出功能。

新的類FormsWithTimeoutAuthentication繼承FormsAuthentication
public class FormsWithTimeoutAuthentication : FormsAuthentication    {        private DispatcherTimer idleTimer;        private int minutesIdle;        private bool idle;        private bool attached = false;        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;         }         protected override LoginResult EndLogin(IAsyncResult asyncResult)         {             var result = base.EndLogin(asyncResult);             if (result.LoginSuccess == true)             {                 if (!attached) AttachEvents();                 minutesIdle = 0;                 idleTimer.Start();             }             return result;         }         protected override LogoutResult EndLogout(IAsyncResult asyncResult)         {             idleTimer.Stop();             return base.EndLogout(asyncResult);         }         private void AttachEvents()         {             attached = true;             Application.Current.RootVisual.MouseMove += new MouseEventHandler(RootVisual_MouseMove);             Application.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);         }         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;         }         private void Logout()         {             //這裡是你自己的退出登入代碼,我這裡是調用JS,重新整理頁面而已              HtmlPage.Window.Invoke("RefreshSL", "Invoke");         }     }
修改App.xaml,修改WebContext表單認證模式
 1 <Application 
2 x:Class="MyAppSL.App"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:app="clr-namespace:MyAppSL"
6 xmlns:appService="clr-namespace:MyAppSL.Services"
7 xmlns:Lng="clr-namespace:MyAppSL.Resources"
8 xmlns:appsvc="clr-namespace:System.ServiceModel.DomainServices.Client.ApplicationServices;assembly=System.ServiceModel.DomainServices.Client.Web"
9 Startup="Application_Startup"
10 UnhandledException="Application_UnhandledException">
11
12 <Application.ApplicationLifetimeObjects>
13 <app:WebContext>
14 <app:WebContext.Authentication>
15 <appService:FormsWithTimeoutAuthentication IdleMinutesBeforeTimeout="2"/>
16 </app:WebContext.Authentication>
17 </app:WebContext>
18 </Application.ApplicationLifetimeObjects>
19
20 <!--原來是這樣的-->
21 <!--<Application.ApplicationLifetimeObjects>
22 <app:WebContext>
23 <app:WebContext.Authentication>
24 <appsvc:FormsAuthentication />
25 </app:WebContext.Authentication>
26 </app:WebContext>
27 </Application.ApplicationLifetimeObjects>-->
28
29  </Application>

我這裡配置的Idle兩分鐘就逾時退出登入,可以自己配置逾時時間。實現的效果就是使用者登入Silverlight應用以後,如果一定的時間沒有動作(Idle)就逾時退出登入,返回登入頁面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.