This tutorial series takes you through how Azure Mobile Services provides Mobile-Backend-As-A-Service by discussing various features and basics of how to get started.
Authentication and Authorization is a key component of any app. One of the most powerful capabilities of Azure Mobile Services is how easy it is to add user authentication to mobile apps. Again, the value proposition comes from a cross platform support, also the authentication can be done using several third party identity service providers such as Facebook, Google, Microsoft, Twitter and Azure Active Directories. The basic idea is to register your app with one of the various identity service provider’s developer site which provides you with the key id and a secret. This information is fed into the mobile services portal and then the app can use the services of the identification provider to authenticate.
The flow of the authentication procedure is shown in the diagram below. Authentication uses OAuth behind the scenes which is otherwise quite a complex procedure and Mobile Services does a great job at abstracting all the details. Mobile Service is registered with identity provider to provide authentication. The client calls the auth method and passes in the provider name. Mobile services and the provider does OAuth to authenticate the users. The UserId and token are returned to the client and is stored and is accessible from the Mobile services.
In order to authenticate and authorize users, there are few basic steps:
Register App for authentication and configure Mobile Services
Restrict Permissions to authenticate users
Add Authentication to app
Use scripts to authorize users in Mobile Services
Registration of the app can be done in two steps. First, one needs to go and register the app name at the identity provider’s developer site. Once the registration is done, the package id and the client secret yielded is entered in the portal. For example, if you want to authenticate a Live Id, you would be registering your app in the Windows Dev Centre and getting the client id and secret from the App Settings:
This package id, client id and secret needs to be entered in the Azure Mobile Services Portal as shown below under the identity tab.
Once the registration part is done, one needs to restrict the permissions on the users. Under the data tab in the portal, we need to go to the table and click on permissions. In the permissions tab, we need to change the option to “only authenticated users” for each of the four operations of insert, update, delete and read.
Now let us add the piece which will authenticate the user and give access to the data. Let us look into the sample code that needs to be added to your app. Essentially, we have to connect to the identity provider, enter user credentials. An object for the user id needs to be initialized and a method needs to be added which will call the identy provider. If using a Windows Universal App, the following code snippet does the trick:
The value of MobileServiceAuthenticationProvider can be changed to the Identity service provider that you want to use for authentication. Once this is added, the method needs to be called from the onNavigated method:
That’s it, now you only allow authenticated users to access your data.
Now that authentication is taken care of, let us see how do we authorize users to access their own data using server scripts. We need to register scripts in the portal itself and is done against each of the operations of insert, delete, read and update. We need to go to the portal, select the table and then click on script which will take you to the script options.
Replace the insert script with the following to add a userID column:
The Read operation can be replaced as follows:
That sets up insert and read operation for authorized users.
In this section, we see how easy it is to integrate authentication and authorization to your app. We discussed the concept which primarily uses OAuth in the backend, abstracting the details from us. We also saw the various third party identity service providers using which we authenticate the users. There are few other forms of authentication such as Single Sign-on and Multi factor authentication which can be looked into as per your interest. Please reach out to me @AdarshaDatta and share your stories with me about Azure Mobile Services.