DEV Community

Mike James
Mike James

Posted on • Originally published at mikecodes.net on

Getting started with Data and Auth using Microsoft App Center

Screenshot 2019-06-10 at 08.56.05.png

In my first week working for Xamarin, I was flown to Austin Texas from London in order to help support Xamarin Evolve 2013. Like most conferences, we had sponsors, which included a little startup called Parse. Parse was a backend born in 2011 that allowed mobile developers to easily add cloud storage, push notifications and authentication to their apps. I was sold on the technology immediately and got started building out sample apps to demo Xamarin using parse backends.

Parse was acquired by Facebook in 2013 and lasted until 2016 before being shut down. Thankfully Facebook made Parse open-source which allowed users to migrate their Parse backends to cloud providers such as Azure with our Parse Server template.

Parse no longer having a maintainer was a problem for lots of users, who would want to bet new apps on unmaintained services? I decided that any new apps I developed would not rely on Parse but instead I’d need to find an alternative. Being a .NET Developer, it made sense to use Microsoft’s Azure Mobile App service, which consists of a .NET Framework based web API project and a client SDK.

Embracing Azure Mobile Apps saw me spending more time in Visual Studio writing generic Web APIs and broadening my experience with Web API development. These skills provided me the ability to drop the pre-built solutions and start rolling my own as seen in the Mobile Cloud Workshop I created with my esteemed colleague, Robin-Manuel.

Whilst rolling the backend offers unparalleled flexibility, it’s also no small undertaking to develop, test and maintain, which was ultimately a distraction from my core goal of building a mobile app. I’ve not reached the levels of productivity developing cloud connected apps since migrating from Parse in 2016.

App Center

This has all changed now with the latest update to Microsoft App Center. If you’ve not heard of App Center, it’s the replacement to HockeyApp or Xamarin Insights. It’s our automated build, test and analytics platform, designed specifically for mobile developers.

App Center has proved invaluable for building better apps but it wasn’t a ‘one stop shop’ for mobile dev, with me having to roll my own backends for data and authentication. This changed earlier this year at Build, with the addition of authentication and data storage features.

Authentication

Screenshot 2019-05-23 at 12.24.18.png

Authentication is one of those features that I like to avoid as its unexciting and easy to get wrong. It’s this reason that I never roll my own authentication solution, but instead rely on 3rd parties like Active Directory or Auth0.

When I first started using Azure Mobile Apps with its companion mobile SDK, I was able to authenticate users with only a couple of lines of code, which made it a no brainer to add to my demos. I’m super happy to say that the new Mobile Apps SDK makes it just as easy! It does this by wrapping the Microsoft Authentication Library (MSAL) which implements authentication using OAuth 2.0.

token-exchange-service

The authentication process is powered by Active Directory B2C (Business to Consumer), which makes it’s incredibly easy to setup! It also makes it possible to use a huge variety of identity providers such as Google, Twitter, Facebook and Microsoft as well as integrate with 3rd party providers. It gets all of the standard B2C functionality, such as using machine learning to detect suspicious behaviour and options for multi-factor auth.

Once a user has authenticated, they can create documents using App Center Data which are only visible to them.

Data

Screenshot 2019-05-31 at 17.26.37

The biggest difference between App Center and Azure Mobile Apps has to be how we model andstore data.) With the older Azure Mobile Apps, we could pick between Microsoft Azure SQL or for testing, a SQLite Database. The only option we had was to a relational database, which was great if you had a schema that lends itself well to being normalised.

App Center uses Cosmos DB as its underlying data storage service. This is our cloud-first, global scale NoSQL database offering. Cosmos DB allows us to forget about database scaling as it promises low latency and high availability in all the regions you deploy your data to.

data-architecture

Partitioning Data

App Center only uses one collection which helps keeps cost down as we pay for every collection, even if empty when using Cosmos DB. Within the single collection used for App Center, you can define two types of document, private and public.

Private documents consist of user data, allowing authenticated users to have read and write access to edit the contents.

Public documents only allow for read access (using the App Center SDK) as to avoid users being able to overwrite global data.

data-partitions

Offline

One of my favourite features of using Azure Mobile Apps was its ability to support offline-synchronization of data between client and the backend. Offline sync not only allows apps to work offline but also makes them appear more performant when online.

App Center supports offline read and writes but the data is only cached for a day by default. It’s possible to set a time-to-live (TTL) if required. If connectivity is lost, users will still have the ability to read cached documents and write data with changes cached until connectivity is restored. One important thing to note is that conflicts are handled using “last write wins”.

Schema

If I use the App Center Data SDK then I can use plain CLR objects with the only requirement that I have an ID property of type Guid.

public Guid Id { get; set; } = Guid.NewGuid();

When the object is actually saved to App Center, you’ll see that the schema is a little different to what you might expect. This is because App Centner add a few extra properties and nests your schema into a object named “document”.

This is very important to remember if you’re going to interact with the data using other services.

Screenshot 2019-06-10 at 10.20.28

Wrapping up

I’ve only just started to explore what I can build with App Center and how best to integrate it into my app development process.

With less than a week of development, I’ve already got an app built which supports push notifications, offline sync of data and authentication. This kind of productivity is a huge win in terms of my ability to add features as I focus on the app and let the App Center team focus on the backend infrastructure.

If you’ve ever used Parse, Firebase or Azure Mobile Apps then I highly recommend giving App Center a try.

Top comments (0)