Login Window Xamarin.Forms UWP App With Blocking Feature If Incorrect Login

In this article, we are going to create the login window application that will take UserId and Password as its credentials and prompt success if correct credentials are passed.

Introduction

In this article, we are going to create the login window application that will take UserId and Password as its credentials and prompt success if correct credentials are passed.

If the user clicks on the Login button three times with incorrect credentials, then it will block the UI for 30 seconds.

Note

This application will store the time in Windows.storage and if the application is closed and restarted, then it will start its delay from where it is ending.

Eg -

Suppose after 3 incorrect login attempts, if the user is requested to wait for 30 seconds and in the meantime, the user has closed the application after 5 seconds and restarted the application, then it will start from the remaining time and let the user wait for the specific remaining time.

Implementation

Open Visual Studio and select "New Project...".

Xamarin

Select "CrossPlatform App (Xamarin)".

Xamarin

Select Shared Project/PCL.

Xamarin

Xamarin

Set the minimum version and target version as given.

Xamarin

From the configuration window, check on the Build and Deploy for UWP.

Now, on the Shared project / PCL (LoginDemoXamarinForm ), add the Xamarin.Forms content page (eg: Page1.xaml).

Xamarin

Added the source code only for LoginDemoXamarinForm.

The directory structure will be as follows.

Xamarin

Open Page1.xaml and modify the content as below.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="LoginDemoXamarinForm.Page1">  
  5.   
  6.         <ContentPage.Content>  
  7.             <StackLayout Spacing="10" VerticalOptions="CenterAndExpand" BackgroundColor="BlanchedAlmond">  
  8.                 <Entry x:Name="userId" Placeholder="UserId" Text=""></Entry>  
  9.                 <Entry IsPassword="True" x:Name="password" Placeholder="Password" Text=""></Entry>  
  10.                 <Button x:Name="loginBtn" Text="Login"></Button>  
  11.                 <Label x:Name="message" Text=""></Label>  
  12.             </StackLayout>  
  13.         </ContentPage.Content>  
  14.   
  15. </ContentPage>  

Open Page1.xaml.cs and modify the content as below.

  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Xaml;  
  5.   
  6. namespace LoginDemoXamarinForm  
  7. {  
  8.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  9.     public partial class Page1 : ContentPage  
  10.     {  
  11.         private int maxCount;  
  12.         int counter;  
  13.         public Page1()  
  14.         {  
  15.             InitializeComponent();  
  16.   
  17.             // check the retry time on starting  
  18.             var seconds = TimeSpan.FromSeconds(0);  
  19.             Device.StartTimer(seconds, () => {  
  20.                 WaitOnStart();  
  21.                 return false;  
  22.             });  
  23.   
  24.             counter = 0;  
  25.             userId.Focused += (e,a) => {  
  26.                 message.Text = "";  
  27.             };  
  28.             password.Focused += (e, a) => {  
  29.                 message.Text = "";  
  30.             };  
  31.             loginBtn.Clicked += (e, args) =>  
  32.             {  
  33.                 {  
  34.                     if (userId.Text.Equals("") || password.Text.Equals(""))  
  35.                     {  
  36.                         message.Text = "Fill the required fields";  
  37.                         return;  
  38.                     }  
  39.                     if (userId.Text.Trim().Equals("Irshad") && password.Text.Trim().Equals("Irshad"))  
  40.                     {  
  41.                         counter = 0;  
  42.                         message.Text = "";  
  43.                         DisplayAlert("Login""Success""Ok");  
  44.                     }  
  45.                     else  
  46.                     {  
  47.                         message.Text = "Incorrect Credentials!";  
  48.                         counter++;  
  49.                         if (counter == 3)  
  50.                         {  
  51.                             WaitTime(30);  
  52.                             counter = 0;  
  53.                         }  
  54.                     }  
  55.                 };  
  56.             };  
  57.         }  
  58.   
  59.         protected async void WaitTime(int wt)  
  60.         {  
  61.             Windows.Storage.ApplicationDataContainer localSetting = Windows.Storage.ApplicationData.Current.LocalSettings;  
  62.             localSetting.Values["timeDelay"] = DateTime.Now.AddSeconds(wt).ToString();  
  63.             loginBtn.IsEnabled = false;  
  64.             userId.IsEnabled = false;  
  65.             password.IsEnabled = false;  
  66.             maxCount = wt;  
  67.             while (maxCount > 0)  
  68.             {  
  69.                 await Task.Delay(1000);  
  70.                 message.Text = string.Format("Retry after {0} seconds!!", maxCount--);  
  71.                 if (maxCount == 0)  
  72.                 {  
  73.                     message.Text = "";  
  74.                     loginBtn.IsEnabled = true;  
  75.                     userId.IsEnabled = true;  
  76.                     password.IsEnabled = true;  
  77.                 }  
  78.             }  
  79.   
  80.         }  
  81.         public void WaitOnStart()  
  82.         {  
  83.             Windows.Storage.ApplicationDataContainer localSetting = Windows.Storage.ApplicationData.Current.LocalSettings;  
  84.             if (localSetting.Values["timeDelay"] != null)  
  85.             {  
  86.                 DateTime dt = Convert.ToDateTime(localSetting.Values["timeDelay"].ToString());  
  87.                 if (dt > DateTime.Now)  
  88.                 {  
  89.                     userId.IsEnabled = false;  
  90.                     password.IsEnabled = false;  
  91.                     loginBtn.IsEnabled = false;  
  92.                     TimeSpan result = dt.Subtract(DateTime.Now);  
  93.                     maxCount = Convert.ToInt16(result.TotalSeconds);  
  94.                     WaitTime(maxCount);  
  95.                 }  
  96.             }  
  97.         }  
  98.     }  
  99. }  

Open App.xaml.cs of LoginDemoXamarinForm project and modify the below content.

Xamarin

Just set the MainPage to the added page name Page1.

Execute the application.

Xamarin

Xamarin

Xamarin

If you click Login button three times with incorrect credentials, then it will block the UI and make you wait for 30 seconds.

When you provide the correct credentials, then the below alert message will appear.

Xamarin

Important points

  1. Use of WaitOnStart()
    This method is used on the starting of the application. This will check the required delay time for the user input. If there is any delay time available apart from the required 30 seconds then it will, first of all, let you wait for the remaining time. It will fetch the time from the Windows.Storage.ApplicationDataContainer.
  1. Use of WaitTime(int wt)
    This method is responsible to enable/disable the controls of UI on the basis of the incorrect credentials provided and also it sets the value for Windows.Storage.ApplicationDataContainer for the remaining time.
  1. For better understanding follow the below steps,

    1. Run the application.
    2. Provide wrong credentials.
    3. Click three times login button to Disable the functionality.
    4. Close the application after some seconds (5 or 6).
    5. Again run the application.
    6. You will see the message “Retry after n seconds”. Where n is the remaining value of the delay time.