Loading...

Xamarin.Android - Make a Finger Print Authentication Applica

View: 150    Dowload: 0   Comment: 0   Post by: admin   Category: Android   Fields: Other

Download  xamarin-android-make-a.zip (105.60 KB)

You need to Sign In to download the file xamarin-android-make-a.zip
If you do not have an account then Sign up for free here

In this article, I’m going to show you exactly how to implement fingerprint authentication in your own apps, by walking you through the process of creating a sample app that registers when the user places their fingertip against their device’s touch sensor, processes their input, and then displays a

Introduction

Hy friends, many days ago, i was thinking to develop a android fingerprint auth app with Xamarin because nowadays, every mobile have a physical touch sensor feature so, there was a need to develop a custom fingerprint app for android phones using C# and Xml.

In this article, I’m going to show you exactly how to implement fingerprint authentication in your own apps, by walking you through the process of creating a sample app that registers when the user places their fingertip against their device’s touch sensor, processes their input, and then displays a range of toasts depending on whether the fingerprint authentication has succeeded or failed. We’ll also be looking at how to test fingerprint authentication on Android Virtual Devices (AVDs) that don’t feature a physical touch sensor.

The prerequisites

  • Android Support Library v7 AppCompat
  • Fingerprint Image

The steps given below are required to be followed in order to create a Fingerprint Authentication app in Xamarin.Android, using Visual Studio.

Step 1 - Creating Fingerprint Authentication Project

Let's create your Android solution in Visual Studio or Xamarin Studio. Select Android and from list choose Android Blank App give it a name like, FingerAuth.

Step 2 - Add Fingerprint to Emulator

First we will add our fingerprint to AVD - (I suggest you use Android Studio's Emulator for this example).

  1. Go to your Emulator -> Settings -> Security -> FingerPrint -> Add -> Set your PIN (for example 1234) -> Next Find your Sensor.
     
  2. Now go to Start and open Cmd as Administrator then change directory (cd) so it’s pointing at your Android SDK download; specifically, the Android/sdk/platform-tools folder.

    My command looks like this,

    cd C:\Program Files (x86)\Android\android-sdk\platform-tools
     
  3. Add your frintprint by typing new cmd (adb -e emu finger touch 1234abc). In this example I am setting up fingerprint 1234abc.

Step 3 - Add Android Support v7 AppCompat Library

First of all, in References add a reference Android.Support.v7. AppCompat using NuGet Package Manager, as shown below.

 

Step 4 - Add Finger Print Image

Open Solution Explorer -> Project Name -> Resources-> Drawable. Fingerprint image paste into drawable folder.

Step 5 - Add XML File

After successful installation of AppCompat, add a new XML file. For that, go to Solution Explorer-> Project Name-> Resources-> Values-> Right-click to add a new item, select XML, and give it a name, such as Styles.xml. Open this XML file and add the following code.

(Folder Name: values, File Name: Styles.xml)

<?xml version="1.0" encoding="utf-8" ?>   
<resources>  
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">  
  <item name="colorPrimary">#263237</item>  
  <item name="colorPrimaryDark">#263237</item>  
  <item name="colorAccent">#1e282d</item>  
</style>    
</resources> 

Step 6 - Main Layout

Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Open this main layout file and add the following code.

(File Name: Main.axml , Folder Name: Layout)

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    android:background="#263237"  

    android:padding="16dp">  
    <ImageView  

        android:id="@+id/fingerImage"  

        android:src="@drawable/fingerprint"  

        android:layout_centerInParent="true"  

        android:layout_width="150dp"  

        android:layout_height="150dp" />  
    <TextView  

        android:layout_below="@+id/fingerImage"  

        android:textColor="#F5F5F5"  

        android:textSize="20sp"  

        android:text="Please place your fingertip on the scanner to verify your identity"  

        android:layout_centerHorizontal="true"  

        android:textAlignment="center"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content" />  
</RelativeLayout>   

Step 7 - Add Home Layout

Next, add a new layout by going to Solution Explorer-> Project Name-> Resources-> Layout. Right-click to add a new item, select Layout, and give it a name, such as Home.axml. Open this layout file and add the following code.

(Folder Name: Layout , File Name: Home.axml)

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:padding="16dp"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent">  
    <TextView  

        android:textColor="#95aab4"  

        android:textSize="20sp"  

        android:text="You have successfully logged in with Fingerprint Authentication"  

        android:layout_centerHorizontal="true"  

        android:textAlignment="center"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content" />  
</RelativeLayout>  

Step 8 - Create Home Activity

Add a new Activity. For this, open Solution Explorer-> Project Name-> right click to add a new item and select Activity. Give it a name like Home and add the following code putting appropriate namespaces.

(FileName: Home)

using Android.App;  
using Android.OS;  
using Android.Support.V7.App;  
namespace FingerAuth  
{  
    [Activity(Label = "HomeActivity" , Theme ="@style/AppTheme")]  
    public class HomeActivity : AppCompatActivity  
    {  
        protected override void OnCreate(Bundle savedInstanceState)  
        {  
            base.OnCreate(savedInstanceState);  
            // Create your application here  
            SetContentView(Resource.Layout.Home);  
        }  
    }  
}  

Step 9 - Main Activity Class

Now, go to Solution Explorer-> Project Name-> MainActivity and add the following code with appropriate namespaces.

(FileName: MainActivity)

using Android.App;  
using Android.Widget;  
using Android.OS;  
using Android.Support.V7.App;  
using Java.Security;  
using Javax.Crypto;  
using Android.Hardware.Fingerprints;  
using Android.Support.V4.App;  
using Android;  
using System;  
using Android.Security.Keystore;  
namespace FingerAuth  
{  
    [Activity(Label = "FingerAuth", MainLauncher = true, Theme ="@style/AppTheme")]  
    public class MainActivity : AppCompatActivity  
    {  
        private KeyStore keyStore;  
        private Cipher cipher;  
        private string KEY_NAME = "Ahsan";  
        protected override void OnCreate(Bundle savedInstanceState)  
        {  
            base.OnCreate(savedInstanceState);  
            // Set our view from the "main" layout resource  
            SetContentView(Resource.Layout.Main);  
            KeyguardManager keyguardManager = (KeyguardManager)GetSystemService(KeyguardService);  
            FingerprintManager fingerprintManager = (FingerprintManager)GetSystemService(FingerprintService);  
            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.UseFingerprint)  
                != (int)Android.Content.PM.Permission.Granted)  
                return;  
            if (!fingerprintManager.IsHardwareDetected)  
                Toast.MakeText(this, "FingerPrint authentication permission not enable", ToastLength.Short).Show();  
            else  
            {  
                if(!fingerprintManager.HasEnrolledFingerprints)  
                    Toast.MakeText(this, "Register at least one fingerprint in Settings", ToastLength.Short).Show();  
                else  
                {  
                    if (!keyguardManager.IsKeyguardSecure)  
                        Toast.MakeText(this, "Lock screen security not enable in Settings", ToastLength.Short).Show();  
                    else  
                        GenKey();  
                    if (CipherInit())  
                    {  
                        FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);  
                        FingerprintHandler handler = new FingerprintHandler(this);  
                        handler.StartAuthentication(fingerprintManager, cryptoObject);  
                    }  
                }  
            }  
        }  
        private bool CipherInit()  
        {  
            try  
            {  
                cipher = Cipher.GetInstance(KeyProperties.KeyAlgorithmAes  
                    + "/"  
                    + KeyProperties.BlockModeCbc  
                    + "/"  
                    + KeyProperties.EncryptionPaddingPkcs7);  
                keyStore.Load(null);  
                IKey key = (IKey)keyStore.GetKey(KEY_NAME, null);  
                cipher.Init(CipherMode.EncryptMode, key);  
                return true;  
            }  
            catch(Exception ex) { return false; }  
        }  
        private void GenKey()  
        {  
            keyStore = KeyStore.GetInstance("AndroidKeyStore");  
            KeyGenerator keyGenerator = null;  
            keyGenerator = KeyGenerator.GetInstance(KeyProperties.KeyAlgorithmAes, "AndroidKeyStore");  
            keyStore.Load(null);  
            keyGenerator.Init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt)  
                .SetBlockModes(KeyProperties.BlockModeCbc)  
                .SetUserAuthenticationRequired(true)  
                .SetEncryptionPaddings(KeyProperties  
                .EncryptionPaddingPkcs7).Build());  
            keyGenerator.GenerateKey();  
        }  
    }  
}  

Step 10 - Writing FingerprintHandler Class

Go to Solution Explorer-> Project Name and right-click. Select Add -> New Item-> Class. Give it a name like FingerprintHandler.cs and write the following code with appropriate namespaces.

(File Name: FingerprintHandler.cs)

using System;  
using Android;  
using Android.Content;  
using Android.Hardware.Fingerprints;  
using Android.OS;  
using Android.Support.V4.App;  
using Android.Widget;  
namespace FingerAuth  
{  
    internal class FingerprintHandler : FingerprintManager.AuthenticationCallback  
    {  
        private Context mainActivity;  
        public FingerprintHandler(Context mainActivity)  
        {  
            this.mainActivity = mainActivity;  
        }  
        internal void StartAuthentication(FingerprintManager fingerprintManager, FingerprintManager.CryptoObject cryptoObject)  
        {  
            CancellationSignal cancellationSignal = new CancellationSignal();  
            if (ActivityCompat.CheckSelfPermission(mainActivity, Manifest.Permission.UseFingerprint)  
                != (int)Android.Content.PM.Permission.Granted)  
                return;  
            fingerprintManager.Authenticate(cryptoObject, cancellationSignal, 0, this, null);  
        }  
        public override void OnAuthenticationFailed()  
        {  
            Toast.MakeText(mainActivity, "Fingerprint Authentication failed!",ToastLength.Long).Show();  
        }  
        public override void OnAuthenticationSucceeded(FingerprintManager.AuthenticationResult result)  
        {  
            mainActivity.StartActivity(new Intent(mainActivity, typeof(HomeActivity)));  
        }  
    }  
}  

Step 10 - Permission From Device

We need a permission from the device because we shall be using the device’s finger print. Please add Fingerprint permission to your AndroidManifest.xml.Let's open Solution Explorer-> Properties-> AndroidManifest and let's add the code inside application tags.

<uses-permission android:name="android.permission.USE_FINGERPRINT" /> 

Summary

This was the process of creating a Fingerprint Auth app in Xamarin.Android, using Visual Studio. Please share your comments and feedback.

Xamarin.Android - Make a Finger Print Authentication Applica

In this article, I’m going to show you exactly how to implement fingerprint authentication in your own apps, by walking you through the process of creating a sample app that registers when the user places their fingertip against their device’s touch sensor, processes their input, and then displays a

Posted on 20-03-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Chat App

    Chat App

    View: 130    Download: 3   Comment: 0

    Category: Android     Fields: none

    This is a Chat App for Android mobile. You can make separate chat with you partner. I'm using Mysql Database and Php code and All APIs are develop on PHP (REST APIs). My whatsApp:+918285672453: Email ID: nirbhayiise@gmail.com

  • Tower Defense 3D

    Tower Defense 3D

    View: 123    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    The goal in this game is to protect your territory against the attacking enemies. Place defensive structures on or along their path of attack to prevent them reaching your base. Strategic choice and positioning of defensive elements is an essential...

  • Flap the Bird

    Flap the Bird

    View: 77    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tap your screen to make the bird fly. While you are flying there are destruction that will stop you. Avoid hitting the floor and flying to high this will deduct your life. You can collect coins and life along the way. For more mobile games and...

  • Bad Santa

    Bad Santa

    View: 95    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This is a simple shooting game where the Bad Santa is shooting the bad snowman and reindeer. You can collect coins and life along the way. For more mobile games and tutorials you can visit this site.

  • Flappy Halloween Game

    Flappy Halloween Game

    View: 86    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Flappy Halloween is a simple game, tap the screen to move the player. Shoot the pumpkin in the ring to gain score. Avoid hitting the flying destructions and falling from the ground. Since the Halloween is coming this game is really perfect to play...

  • Tilt Ball Game

    Tilt Ball Game

    View: 71    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This game is simple you need to tilt your device to make the ball move in your own desired direction. Reach the finish line before the time ends and move to the next level.

  • Tanks Dawn of Steel Game

    Tanks Dawn of Steel Game

    View: 105    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tanks Dawn of Steel is a shooting game which you have to defend yourself against incoming enemies for as long as you can. For every enemy that you destroy, you will receive a coin as a score. This game will challenge your skills in aiming and...

  • An Experience about Using Webview in Android Studio 3.0

    An Experience about Using Webview in Android Studio 3.0

    View: 178    Download: 0   Comment: 0

    Category: Android     Fields: Other

    I am blogging on ngocminhtran.com (using WordPress platform). Like most websites, I want to write an Android application to help viewers have a lot of great experiences with my blog.

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd