【问题标题】:How to develop auto logout using Xamarin app如何使用 Xamarin 应用程序开发自动注销
【发布时间】:2019-04-12 00:17:26
【问题描述】:

我必须在 App.xaml.cs 上添加功能才能使其正常工作。我在 OnStart 上添加了功能,但现在它会间歇性地一次又一次地让我退出应用程序。根据下面的代码,我需要做什么才能让它停止这样做。还是我的代码有问题。这是我最新的代码:

  namespace MyApp
  {
   public partial class App : Application
  {
    DateTime _sessionStart;

    public App()
    {
       InitializeComponent();

        DatabaseManager = new DatabaseManager(new DatabaseService());

        HttpManager = new HttpManager(new HTTPService());

        MainPage = new NavigationPage(new LoginPage());
    }

    protected override void OnStart()
    {
        // Handle when your app starts
        _sessionStart = DateTime.Now;

        Device.StartTimer(TimeSpan.FromSeconds(60), () =>
        {
            // Check if 24 hours has elapsed
            if (DateTime.Now > _sessionStart.AddHours(24))
            {
                //logout
                 MainPage = new NavigationPage(new LoginPage());
            }

            return true; // True = Repeat again, False = Stop the timer
        });


    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

【问题讨论】:

  • 如果您尝试注销用户,那么他们可能拥有某种令牌或会话,这些令牌或会话持有他们当前已在登录中进行身份验证的事实。使用多种类型的令牌(如 JWT),您可以指定到期日期,以便令牌只能持续这么长时间。一般来说,前端应该提出一个新的请求,后端应该验证客户端仍然处于活动状态。如果不是,请通过拒绝其身份验证机制来注销客户端。

标签: c# xamarin.ios


【解决方案1】:

尝试通过在 Main 上注册自定义应用程序来跟踪空闲时间(应用程序中没有触摸的时间)。请参阅下面的示例

TimeoutInSeconds 值设置为所需的时间间隔(例如 24 小时)

public class Application
{
    // This is the main entry point of the application.
    static void Main (string[] args)
    {
        UIApplication.Main (args, "MYCUSTOMAPP", "AppDelegate");
    }
}

[Register ("MYCUSTOMAPP")]
public class MYCUSTOMAPP : UIApplication
{ 
    const int TimeoutInSeconds = 1800; // 30 minutes
    NSTimer idleTimer;

    public override void SendEvent (UIEvent uievent)
    {
        base.SendEvent (uievent);

        if (idleTimer == null)
            ResetTimer ();
        var allTouches = uievent.AllTouches;
        if (allTouches != null && allTouches.Count > 0 && ((UITouch)allTouches.First ()).Phase == UITouchPhase.Began)
            ResetTimer ();
    }

    void ResetTimer ()
    {
        if (idleTimer != null)
            idleTimer.Invalidate ();

        idleTimer = NSTimer.CreateScheduledTimer (new TimeSpan (0, 0, TimeoutInSeconds), (t) => TimerExceeded());

    }

    void  TimerExceeded ()
    {
        NSNotificationCenter.DefaultCenter.PostNotificationName ("TimeoutNotification", null);
    }
}

向您的视图控制器添加观察者以将其注销

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        NSNotificationCenter.DefaultCenter.AddObserver(new NSString("TimeoutNotification"), handleInactivityNotification);
    }

    public void handleInactivityNotification(NSNotification notification)
    {
        //Return to login page (Root view controller)
        this.NavigationController.PopToRootViewController(true);
    }

【讨论】:

  • 那么这个自定义应用程序和视图控制器会进入 Main.xaml 页面吗?
  • 这是 Xamarin.Forms 应用程序还是 Xamarin.iOS?
  • Xamarin.iOS 应用程序
  • @mlynn Main.xaml 用于 Xamarin.Froms,Main.cs 用于 Xamarin.iOS。您能澄清一下,以便我可以进一步帮助您吗?
  • 是的,它是 Xamarin.iOS 的 Main.cs。
猜你喜欢
  • 2011-07-27
  • 2019-04-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 2012-06-04
相关资源
最近更新 更多