Skip to main content

Mobile Services from Windows Azure - Data Store and custom script(part 3)

Part 2
In last post we saw how we can create a table using Mobile Services to store GPS position. We discover how we can setup who can make each CRUD operation on our table.
In this post we will continue to see how we can write data and retrieves data from our table. We already created an instance of MobileServiceClient. From this service we will need to retrieve an instance to our table using GetTable method. After we have a reference to our table (IMobileServiceTable) we will be able to execute any kind of request over our table:
IMobileServiceTable tableService = mobileServiceClient.GetTable<Location>();
await tableService.InsertAsync(location);
await tableService.Select(l=> { … });
await tableService.UpdateAsync(location);
I think that you already notified that all the methods are async. Also, you don’t need to manual create a new instance of the IMobileServiceTable. This is handled by the Mobile Services component automatically.
Do you remember how we setup our table? Only authenticate users will be able to execute any kind of action. Because of this we will need to add the authentication mechanism also.
Don’t be afraid, the authentication step is very simple. This mechanism is already implemented by Mobile Services; we only need to call a method that will make the entire job for us. In this moment we have 4 identifier providers that are supported (Google, Facebook, Twitter, Windows Live). You cannot add custom providers in this moment.
await mobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
Calling this method will display to the user the authentication dialog. We don’t need to do anything more than this. When the authentication failed an “InvalidOperationException” will be throw. We can catch this exception and do any kind of action we want.
After this step we will be able to access user information using CurrentUser property of MobileServiceClient. Each request that is made to our table will contain a parameter named ‘user’ with user information. 
We don’t want to permit users to see location of other users. To be able to restrict access of users we will need to define a script. On each CRUD operation over tables from Windows Azure Mobile Services we can define a custom script that will be called before each request will be executed. In this script we can define almost any kind of action we want.
An interesting part of these scripts is the programing language that needs to be used – JavaScript. This is a great think from the development perspective, because you don’t need to know C# or F# to be able to work with these scripts. The script can be added from the management portal and even ad we will have support for intellisense. A lot of libraries from NodeJS are supported. You will not be able to add reference to your own libraries, but you can define your own method. I great think that can be done is the support of calling remote services.
At this step we need to define a custom script for the insert operation. We will add another column to our table that will contains the id of the user. This action will be done from the script because we don’t want client to be able to hack their id.
function insert(item, user, request) {
  item.userId = user.userId;   
  request.execute();
}
As you can see this step is extremely simple.The second parameter is send each time, even if the user is not login and contain the user information. The table will contain a column named userId with the id of the user.
When someone will do a query we want to retrieve items of the user that make the request. To be able to do this, on the read operation of a table we will add to the query a condition that will retrieve items of the current user.
function read(query, user, request) {
   query.where({ userId: user.userId });   
   request.execute();
}
In this moment we saw how we can execute request over tables from Windows Azure Mobile Services, how we can define custom action over them.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...