Xamarin user inactivity


  • How do you initiate automatic logout when a user is inactive in Blazor?
  • Local Security Measures in Xamarin.Forms
  • Auto Logout or Expire the session after 5 minutes of user inactivity
  • Building a Xamarin.Forms Moon Phase App
  • Using Xamarin Inspector With Your Live Apps
  • Xamarin Forms for Windows Developers: Tips, Tricks and Lessons Learned, Part 2
  • How do you initiate automatic logout when a user is inactive in Blazor?

    Many of these things are things an Android or iOS developer i. Some of these things will be super-extra important, and some will be meaningless to almost everyone.

    Have you heard of HockeyApp? Mostly based off the original HockeyApp services, Mobile Center provides a bunch of services needed by mobile developers, into a single, integrated product. Imagine having a service that will automatically build, test, and then distribute your app, every time you commit a build to GitHub or Team Services. Imagine getting full stack traces and analytics for everything that happens with your app and users.

    Imagine having your app tested on over devices before it goes live, while you binge watch Gilmore Girls. Stop imaging. Start using Visual Studio Mobile Center. Even though Xamarin Forms is the wrong platform for graphic-intensive gaming, there are a number of scenarios where you just plain need a reliable timer.

    One example I use all the time is to lock a user out after periods of inactivity. The Device. StartTimer starts a recurring timer using the device clock capabilities, and is simple to use, easily supports async, and is ultra-reliable. Just use Device. The ever-reliable Device. So many examples of using Xamarin Forms, especially for creating controls, spin up and spit out StackPanels and Labels, and all kinds of junk via code-behind.

    I mean, if you want to do it for fun, just to prove you can, do it. In fact, do it three times, and then never do it again. This means models, views pages and view models. This also means using bindings, converters, and commanding. I know, you are a developer, which means you have a huge ego, a paradoxically low feeling of self worth, and like to be in control. Yes, this is true, and the pun is intended. Using AbsoluteLayout is messy and support for layout properties changes with the wind.

    There are other, better ways to do what you want to do. Yes, there are rare exceptions, like using Windows 10 Home and not having Hyper-V support. However, in general, Android needs a lot of junk in its trunk, and therefore Xamarin Forms needs all that junk to run and compile on Android devices.

    Now if you are super-smart in the Android development world you are going to say this is dumb, and you really only need X. I mean do I really need Hot Tamale What about Lemonhead 5. A great excuse to walk away from your computer for a few days. Windows developers need to use these emulators for a whole bunch of reasons, and the VS Emulators actually work better and are lightning fast on the typical Windows 10 development workstation.

    If you were paying super-close attention in this post, you may have noticed a beautiful, well-written haiku. The first person to locate and verify my magical poetry will win a prize. See you in Part 3. XamarinRocks Need Xamarin Help?

    Local Security Measures in Xamarin.Forms

    Local Security Measures in Xamarin. Forms Posted on by Judson Bandy There are many types of mobile apps where security is of the utmost importance. When dealing with banking, payment, health, or other sensitive information, it is crucial to keep data safe.

    There may even be legal ramifications for exposing user data. This conversation typically begins with a secure login, protecting data in transit, and data access. How can we best protect users from exposing data locally? Yes, a secure login is the first step, but what if a user logs in, and then exits the app without hard closing it.

    If someone else uses their phone, are we able to protect the data? Also, what if they put their phone down with the app still up? There are several little steps that can be done to help the user protect their data. Create the Xamarin. Forms Project First, we will create a Xamarin. Forms project. I am doing it on a Windows computer, but it can also be done on Visual Studio for Mac.

    Open Visual Studio and click to create a new project. Select Mobile App Xamarin. Form in C and press Next. On the template selection, we are going with Blank, and Android and iOS for platforms.

    The app is now created. Now, we are just going to make a simple two page application to show how our security features can be plugged in. Open MainPage. This is the first page that opens when you start the app and it will be our mock login page. Start by deleting everything inside the StackLayout and add the following.

    The button has a function that we will define in the codebehind when it is clicked. Open the MainPage. Define our new method like this. Now, we have to create the new page. Replace the StackLayout with the following. There is a label at the top that talks about all of our secure data, and then, a button at the bottom. Again, we need to go to the codebehind to define our Clicked method. Open SecurePage. This will happen if a user is logged in, exits the app but does not hard close , then clicks to reopen the app.

    If the user is only gone for a short period of time, it should be fine to keep the user logged in. Otherwise, we certainly want to auto logout the user. This one should be fairly straightforward. First, open App. Add the following code. IsUserLoggedIn return; Application. One is SleepTime, which keeps track of time away from the app, and the other is IsUserLoggedIn, which tracks if the user is logged in.

    Then, we override the OnSleep method to set the SleepTime variable to the time that the app went to sleep. Next, we override OnResume. There, we check to see how long we have been away from the app, and if it was longer than 2 minutes, we call the LogOutUser method. In that method, we check to see that the user is logged in, and if so, we return the app to the login page.

    Finally, we need to adjust the IsUserLoggedIn method when the user logs in and out. The user will be able to resume their session if they return to the app within 2 minutes of closing it. Otherwise, they will be auto logged out and have to log in again to access their data. Auto Logout for Inactivity That is an excellent solution for a user that exits the app and then returns, but what if the user leaves their phone with the app still up? Sensitive data could easily be exposed.

    To try to prevent this, we would like to auto logout the user after a certain amount of inactivity. Unfortunately, this code is a little more complicated. This is due to the fact that user input is detected in the Android and iOS sections, respectively, depending on your device type.

    Android Inactivity Open MainActivity. Our strategy will be to throw a popup after a certain amount of inactivity, and then if there is no activity, log the user out. First, the warning amount of time will be 4 minutes, and then the actual logout will occur if there is no activity for another minute. Next, add the following code to the OnCreate method. This is still inside the MainActivity class. Then, we will add our code to start and stop the inactivity handlers.

    Object, Handler. WarnUserAboutInactivity ; mainActivity. RemoveMessages 1 ; DisconnectWarningHandler. OnUserInteraction is where we are able to register user activity. When the user does anything, we reset our warning timer and stop our disconnect timer.

    Only one of these timers is ever actually on a once, but this does a full reset if one or none of the timers are running. The ResetWarningTimer method calls to send an empty delayed message for the disconnect warning length of time that we set.

    There, we call the App. The disconnect timer does a PostDelayed for the length of time that we set for disconnect. This performs an action that we call LogoutAction, which calls the App. OnStop and OnResume are also overridden so that the timers are reset when you exit and reenter the app. Finally, go back to App. We have to add the inactivity warning method that we called from MainActivity. IsUserLoggedIn return; await Application. DisplayAlert "Inactivity Warning", "Are you still there?

    If you don't return to the application, you will be logged out. The Android solution is done. I would strongly suggest using custom popups instead of the built in DisplayAlert popups. There is no way to programmatically close these popups, which can be problematic in a number of different situations. For instance, you would likely want to close the warning popup about the impending logout once the user is actually logged out.

    There also can be issues with multiple popups being displayed at once. In the iOS section of the project, open AppDelegate. Failed; this. If TouchDetected is true, the LogoutCounter is reset. Otherwise, the inactivity warning method in App. Start interacting with the application and nothing will happen. Otherwise, you will be auto logged out after another minute of inactivity. Obscure Information When Looking at Recent Apps The final issue we want to tackle is the view that shows recently open apps.

    On iOS this is called the App Switcher, and on new iPhone and iPad devices, you get to this screen by swiping up from the bottom to the middle of the screen and holding.

    Regardless of platform, this view shows recently open apps that are currently in the background. Each app shows a picture of your last screen in the app. You are able to click on the picture to reenter an app or you can hard close the app. The issue here is information potentially shown on the picture. If we were on a page containing sensitive information when we exited the app, that sensitive information would now be visible. To prevent this, we are going to obscure what the user sees.

    In the OnCreate method in MainActivity. SetFlags WindowManagerFlags. Secure, WindowManagerFlags. If you go to the recent apps overview, you will see that it shows a white screen with no content, which you can see below. Besides hiding the display, this flag also prevents the user from taking screenshots inside the app. First add a private instance variable of UIView. FromRGB 60,, ; window.

    Auto Logout or Expire the session after 5 minutes of user inactivity

    Now, run your project and you will see a new icon next to the pause and stop button. Pressing this, will launch the inspector. Live Visual Tree When the Inspector loads, if you change the tab at the bottom to View Inspector, you will be presented with a live visual representation of all the UI layers in your app.

    Building a Xamarin.Forms Moon Phase App

    If you hold down your cursor in the image, you can move it around in 3D. This allows you to view your UI at different angles, to gain a better view of the layers. Real Time Changes In the right hand pane, you can see the all the visual elements and their properties.

    If you select a UI element on the Visual Rendering, you will notice it highlights, then shows just that elements property on the right. Note: This can take a few seconds to update.

    If I update the color of the UILabel and press enter, it will update the app my running app. This allows real time updates to your app, which is very handy for testing small changes. Utc ; The Moon class has a Calculate method that accepts a date-time and a hemisphere.

    NET Standard project. We can now launch into developing our mobile interface. Forms, so there is likely a different approach.

    Using Xamarin Inspector With Your Live Apps

    Explore the solution, and feel free to submit updates and improvements. Keeping our Moon interface in mind, we need to accept a date-time and a hemisphere from our user.

    When the user enters values, we need to bind PhaseResult to our UI. GetValues typeof Earth. Add Earth. A note for folks using Rider to develop Xamarin.

    Xamarin Forms for Windows Developers: Tips, Tricks and Lessons Learned, Part 2

    Forms apps for the first time. Rider will recognize our usage of INotifyPropertyChanged and will ask to enhance our project.

    Rider will add a new Annotations. The additional file includes attributes that protect against unintended nulls in addition to hint attributes that can trigger behaviors in Rider. While not in the scope of this post, I suggest folks explore JetBrains. We can assign our binding context using the ContentPage.


    thoughts on “Xamarin user inactivity

    Leave a Reply

    Your email address will not be published. Required fields are marked *