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(

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provided a too