Azure Mobile App Offline Sync - Various Users

Current situation

I created an app that uses the Azure SDK to sync offline data. Synchronization works fine, including user authentication through Microsoft account. On Azure, I am currently using Easy Tables.

Problem

Now when I log in with two different users from two different test devices, I can see the same data. My expectation would be that the authenticated user only sees what has been added / updated with his own user ID.

Topical issue (s)

By design that all authenticated users see data from all other users? So should I add a user id column (based on Microsoft account id) and add a condition to the where clause when getting data? (which is weird, since all data will be synced across all users)

I have looked through the docs but could not find the information I needed. I haven't added any code to this question yet, because it is not clear to me if this is by design, if it is something on the client side, or if a server side config needs to be set up for it.

+3


source to share


2 answers


Synchronization works fine, including user authentication through Microsoft account. On Azure, I am currently using Easy Tables.

"Easy Tables" works with Node.js backend. When you add tables via Easy Tables, it helps you automatically create your Node.js backend for your added tables. You can use "DEVELOPMENT TOOL> App Service Editor (Preview)" under your mobile app in the azure portal to test your server.

By design that all authenticated users see data from all other users? So, do I need to add a user id column (based on my Microsoft account id) and add a condition to the where clause when getting data?

For the mobile client, you can use the Azure Mobile Client SDK to connect to your azure mobile app. Let's assume you are using server managed authentication and use the following code to sign in to your Microsoft account:

MobileServiceUser user=wait MobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);

      

You can get registered user information through the MobileServiceClient.CurrentUser

as CurrentUser

has property UserId

. Since you are using Easy Table, you can simply add a column UserId

to each of your custom tables through the Azure Portal. Also, you need to modify the related SQLite tables in your mobile client app.

For offline synchronization, you can define your OData query as follows to get items owned by a specified user from a remote table.

await todoTable.PullAsync("incsync_ToDoTable_CurrentUser", todoTable.CreateQuery().Where(t => t.UserId=="<userId>");

      

For more information, I recommend you refer to adrian hall's book Data Access and Offline Synchronization .

UPDATE:



For questions:

1) Filter criterion evaluated on the backend and only rows with the given user are synchronized, you can use Fiddler to keep track of the network trace while working with pull

.

2) . For a safer approach, you can customize the data that is sent to the table controller before it is stored on the server side, instead of being pointed UserId

to in your mobile client. For Node.js backend, you can modify your code like this before inserting data:

table.insert(function (context) {
  context.item.userId = context.user.id;
  return context.execute();
});

      

For more information, you can refer to 30 DAYS ZUMO.V2 (AZURE MOBILE APPS): DAY 6 - PERSONAL TABLES .

Also, for C # backend, you can get UserId

like this:

public string UserId
{
    get
    {
        var principal = this.User as ClaimsPrincipal;
        return principal.FindFirst(ClaimTypes.NameIdentifier).Value;
    }
}

      

And change the action to add new items like this:

// POST tables/TodoItem
public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
{
    item.UserId = UserId;
    TodoItem current = await InsertAsync(item);
    return CreatedAtRoute("Tables", new { id = current.Id }, current);
}

      

For more information, you can refer to "User Data" .

+1


source


To filter records for each user, you must add a UserId column to your table, which contains the ID of the user who owns this item.

Then you can either filter your tables in your client application to get only those items of the current user.



Or even better, apply a server-side filter so that your client application only gets user items that are logged in.

But I'm not sure if this is possible with simple tables, you might have to implement a "full" version of the server as in the "backend" section of this repo: https://github.com/Azure/azure-mobile-apps-quickstarts

0


source







All Articles