Making Android App with Xamarin 3 – Connecting with ASP.Net Identity

Many of ASP.Net guys or girls are taking great feature of ASP.Net Identity to manage credential of their web services.  When offering same service via App, it is nice to be able to use the same credential management.

Continue on from  Making Android App with Xamarin 2 – Get InsntaceID. Now that we know how to get InsntaceID to identify the app, we will connect this ID with ASP.net Identity.

When you look at List of tables and how ASP.Net Identity uses them, there is a table called ASPNetUserLogins, where looks like they keep a name of LoginProvider, key issued by the provider, and User ID.

We will treat Android App as just another LoginProvider and store data here.  Then you can actually handle login just like Facebook, Twitter, Microsoft etc.

Although in case for Facebook, Twitter, Microsoft, the authentication is done on their end, and in return the service get Provider Key.  For the app we will have to do the credential of our own using ASP.Net Identity.

To do this, I have to create API service that handles credential checking. Back at my web service, I ll create service, call it something like https://trainwcoach.com/AndroidAuth/ that takes user name and password and some key code to make sure the request is valid, then returns OK or not OK.  If OK (http status OK) I will have User ID, then I will store that into database along with InstanceID.  I should be hashing this password, but I will get to that later.

Here is my authentication class at the web service side.

Let’s give a try and see

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_100916_045608_pm

OK, works.  From security point of view this code need to be revised but for now move forward to checking to see if the InstanceID is registered to DB.

Right now the DB is clean and empty so whatever I through, I should get not registered.

I am going to create folder called Handlers to handle all sorts of things in future.  In this folder I create new class called LoginHandlers.cs.

Let’s take a look at AspNetUserLogins.

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_100916_050025_pm

Now let’s check and see if ProviderKey – LoginProvider has any match.

Again, I will handle this via API.

Going back to the same class, and adding public ActionResult GetUserIDWithInstanceID(string id, string key);

OK, now that this API is ready, let’s post Instance ID to this API and show the result.

 

Here is my SplashActivity.cs

I have my new LoginActivity.cs and Resources/layout/Login.axml which for now has nothing. (I will cover this next).

I confirm that LoginActivity is loaded as expected.

Let’s next handle login and insert InstanceID into the DB .

 

Leave a Reply