<commit_before>---
layout: post
title: "Developing Mobile Apps with Xamarin Forms and Azure Functions"
description: "Learn how to use Xamarin Forms and Microsoft Azure Functions to develop mobile apps that are supported by serverless functions."
date: 2018-07-19 08:30
category: Technical Guide, Mobile, Xamarin Forms
author:
  name: "<NAME>"
  url: "https://twitter.com/dkrzyczkowski"
  mail: "<EMAIL>"
  avatar: "https://cdn.auth0.com/blog/guest-authors/daniel-krzyczkowski.png"
design:
  bg_color: "#333333"
  image: https://cdn.auth0.com/blog/xamarin/xamarin.png
tags:
- xamarin-forms
- azure-functions
- serverless
- mobile
- azure
- auth0
- hybrid
related:
- 2017-03-10-using-serverless-azure-functions-with-auth0-and-google-apis
- 2016-03-28-xamarin-authentication-and-cross-platform-app-development
- 2018-07-12-building-an-audio-player-app-with-ionic-angular-rxjs-and-ngrx
---

**TL;DR:** The Microsoft Azure Functions is a solution which enables developers running small serverless pieces of code (functions in the cloud) without worrying about a whole application or the infrastructure to run it. They can be used as a backend for web or mobile applications. In this article, we will present how to access an Azure Function secured by Auth0 from a Xamarin Forms application.

---

## Introducing Microsoft Azure Cloud Platform

Microsoft Azure is a platform which provides components to quickly create, implement, and manage cloud solutions. It offers wide range of application, computing, warehouse, network services, and supports all three models of cloud services: 

* [Infrastructure as a Service (IaaS)](https://azure.microsoft.com/en-us/overview/what-is-iaas);
* [Platform as a Service (PaaS)](https://azure.microsoft.com/en-us/overview/what-is-paas);
* and [Software as a Service (SaaS)](https://azure.microsoft.com/en-us/overview/what-is-saas).

Microsoft Azure is available through a [web portal](https://portal.azure.com/) where all mentioned components can be created and configured. In this article, we will use Azure Functions: a PaaS solution that enables developers running small pieces of code without bothering about whole application or the infrastructure to run it.

Creating an Azure account is free of charge but you have to provide some credit card information. There will be no charge, except for a temporary authorisation hold. Microsoft offers $200 credit for start to explore services for 30 days.

If you decide not to upgrade at the end of 30 days or once you've used up your $200 credit, any products you’ve deployed will be decommissioned and you won’t be able to access them. You will not be charged and you can always upgrade your subscription.

## Using Azure Functions as a Serverless Backend

There are many cases where Azure Functions can be used. For example, they can be used to integrate different systems, for data processing, or to build simple APIs and microservices. The Microsoft Azure cloud platform provides different initial templates for Azure Functions. Below there are some examples of them:

* [HTTPTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook)—Triggers the execution of a function code on HTTP requests.
* [TimerTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer)—Executes cleanup or other batch tasks on a predefined interval.
* [QueueTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue)—Respond to messages as they arrive in an Azure Storage queue.

In this article, we will use the HTTPTrigger template to create our Azure Function. This function will be secured by Auth0 so, before accessing it, users will have to authenticate. These users will use (and authenticate through) a Xamarin Forms application which will then submit requests to our Azure Function.

## Creating Azure Functions
You can create the Azure Function in the [Azure portal](https://portal.azure.com/). Follow below steps to create new Azure Function with HTTPTrigger template:

1. Click "Create a resource" button in the left top corner
2. Type "Function app" in the search window
3. Select "Function app" from the list
4. Click "Create" button

In this step, you have to provide few details:

* App name - name of the function app, can be "auth0functionapp" (in my case it was "auth0securedfunction") - name has to be unique.
* Subscription - your active Microsfot Azure subscription selcted
* Resource group - group where function app will be located, here select "Create new" and type "function-app-rg"
* OS - host operating system where function code will be executed, here select "Windows"
* Hosting plan - should be set as "Consumption Plan" so you pay only for the number of executions
* Location - it is good to create app in your region so find your region on the list
* Storage - some functionality connected with function apps requires storage, select "Create new" and leave the name as it is

Once you fill all required, information click "Create" button.

After short time the Function App should be created and notification shows up. Select "Function App" from the left bar. Blade with created function app should appear. Expand it and move mouse cursor on the "Functions" header and click "+" button. 

![Add new function app](https://github.com/Daniel-Krzyczkowski/guest-writer/blob/master/articles/images/auth0_4.png)

New blade with templates will be displayed. Choose "HTTPTrigger C#" template, fill name field with "Auth0FunctionApp" value and change authorization level to "Anonymous". After few seconds HTTPTrigger function is ready for the further implementation.

### Scaffolding the Project
Default template needs some adjustments. Nuget packages are supported by the Function Apps. For an authentication process we will use **Microsoft.IdentityModel.Protocols.OpenIdConnect** NuGet package. Open "View files" tab on the right and click "Add" button. You have to create new json file which will contain information about required NuGet packages. Type "project.json" as the file name and click "Enter". Once file is created you have to define its structure so paste below code and click "Save" button:


```C#
{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.IdentityModel.Protocols.OpenIdConnect": "5.2.2"
      }<commit_msg>## Creating Azure Functions<commit_after>---
layout: post
title: "Developing Mobile Apps with Xamarin Forms and Azure Functions"
description: "Learn how to use Xamarin Forms and Microsoft Azure Functions to develop mobile apps that are supported by serverless functions."
date: 2018-07-19 08:30
category: Technical Guide, Mobile, Xamarin Forms
author:
  name: "<NAME>"
  url: "https://twitter.com/dkrzyczkowski"
  mail: "<EMAIL>"
  avatar: "https://cdn.auth0.com/blog/guest-authors/daniel-krzyczkowski.png"
design:
  bg_color: "#333333"
  image: https://cdn.auth0.com/blog/xamarin/xamarin.png
tags:
- xamarin-forms
- azure-functions
- serverless
- mobile
- azure
- auth0
- hybrid
related:
- 2017-03-10-using-serverless-azure-functions-with-auth0-and-google-apis
- 2016-03-28-xamarin-authentication-and-cross-platform-app-development
- 2018-07-12-building-an-audio-player-app-with-ionic-angular-rxjs-and-ngrx
---

**TL;DR:** The Microsoft Azure Functions is a solution which enables developers running small serverless pieces of code (functions in the cloud) without worrying about a whole application or the infrastructure to run it. They can be used as a backend for web or mobile applications. In this article, we will present how to access an Azure Function secured by Auth0 from a Xamarin Forms application.

---

## Introducing Microsoft Azure Cloud Platform

Microsoft Azure is a platform which provides components to quickly create, implement, and manage cloud solutions. It offers wide range of application, computing, warehouse, network services, and supports all three models of cloud services: 

* [Infrastructure as a Service (IaaS)](https://azure.microsoft.com/en-us/overview/what-is-iaas);
* [Platform as a Service (PaaS)](https://azure.microsoft.com/en-us/overview/what-is-paas);
* and [Software as a Service (SaaS)](https://azure.microsoft.com/en-us/overview/what-is-saas).

Microsoft Azure is available through a [web portal](https://portal.azure.com/) where all mentioned components can be created and configured. In this article, we will use Azure Functions: a PaaS solution that enables developers running small pieces of code without bothering about whole application or the infrastructure to run it.

Creating an Azure account is free of charge but you have to provide some credit card information. There will be no charge, except for a temporary authorisation hold. Microsoft offers $200 credit for start to explore services for 30 days.

If you decide not to upgrade at the end of 30 days or once you've used up your $200 credit, any products you’ve deployed will be decommissioned and you won’t be able to access them. You will not be charged and you can always upgrade your subscription.

## Using Azure Functions as a Serverless Backend

There are many cases where Azure Functions can be used. For example, they can be used to integrate different systems, for data processing, or to build simple APIs and microservices. The Microsoft Azure cloud platform provides different initial templates for Azure Functions. Below there are some examples of them:

* [HTTPTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook)—Triggers the execution of a function code on HTTP requests.
* [TimerTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer)—Executes cleanup or other batch tasks on a predefined interval.
* [QueueTrigger](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue)—Respond to messages as they arrive in an Azure Storage queue.

In this article, we will use the HTTPTrigger template to create our Azure Function. This function will be secured by Auth0 so, before accessing it, users will have to authenticate. These users will use (and authenticate through) a Xamarin Forms application which will then submit requests to our Azure Function.

## Creating Azure Functions

You can create the Azure Function in the [Azure portal](https://portal.azure.com/). Follow the steps below to create new Azure Function with HTTPTrigger template:

1. Click the "Create a resource" button in the left top corner.
2. Type "Function app" in the search window.
3. Select "Function app" from the list.
4. Click the "Create" button.

In this step, you have to provide few details:

* _App name_: This will be the name of the function app. You can add something like "auth0-function-app". In my case, I used "auth0securedfunction" but you can't use the same as the name has to be unique.
* _Subscription_: This is the type of the subcscription that you have.
* _Resource group_: This will be the group where function app will be located. You can select "Create new" and type "function-app-rg".
* _OS_: As the host operating system, you can select "Windows".
* _Hosting plan_: You can set it as "Consumption Plan" so you pay only for the number of executions.
* _Location_: It is a good idea to create the function in a region near you.
* _Storage_: Some functionalities connected with Azure Functions require storage. You can just select "Create new" and leave the name as it is.

Once you fill all the required information click the "Create" button.

After a short time the Function App will be created and notification will show up. After that, select "Function App" from the left bar. A blade with the created function app should appear. Expand it and move mouse cursor on the "Functions" header and click the "+" button. 

![Add new function app](https://github.com/Daniel-Krzyczkowski/guest-writer/blob/master/articles/images/auth0_4.png)

Another blade with templates will be displayed. From there, choose "HTTPTrigger C#" template, fill the _name_ field with "Auth0FunctionApp" and change authorization level to "Anonymous". After few seconds, the HTTPTrigger function will be ready for you.

### Scaffolding the Project
Default template needs some adjustments. Nuget packages are supported by the Function Apps. For an authentication process we will use **Microsoft.IdentityModel.Protocols.OpenIdConnect** NuGet package. Open "View files" tab on the right and click "Add" button. You have to create new json file which will contain information about required NuGet packages. Type "project.json" as the file name and click "Enter". Once file is created you have to define its structure so paste below code and click "Save" button:


```C#
{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.IdentityModel.Protocols.OpenIdConnect": "5.2.2"
      }
{'fraction_non_alphanumeric': 0.07484051657071729, 'fraction_numerical': 0.011202738447175977, 'mean_word_length': 4.540948275862069, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 24, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2658158', 'n_tokens_mistral': 3452, 'n_tokens_neox': 3254, 'n_words': 1650}