c# - Azure移动应用程序离线同步-不同用户

标签 c# azure azure-mobile-services

现状

我创建了一个使用 Azure SDK 进行离线数据同步的应用程序。同步工作正常,包括通过 Microsoft 帐户进行用户身份验证。在 Azure 中,我目前使用 Easy Tables。

问题

现在,当我使用来自两个不同测试设备的两个不同用户登录时,我可以看到相同的数据。我的期望是经过身份验证的用户只能看到使用他自己的用户 ID 添加/更新的内容。

实际问题

所有经过身份验证的用户都可以看到所有其他用户的数据吗?那么,我是否必须添加一些带有用户 ID 的列(基于 Microsoft 帐户 ID),并在获取数据时向 where 子句添加条件? (这很奇怪,因为所有数据都会同步到所有用户)

我正在查看文档,但找不到相关的正确信息。我还没有向这个问题添加任何代码,因为我不清楚这是否是设计使然,是否是客户端的某些内容,或者是否需要调整服务器端的配置。

最佳答案

Sync is working fine including user authentication via Microsoft Account. In Azure I currently use Easy Tables.

Easy Tables 与 Node.js 后端配合使用,当通过 Easy Tables 添加表时,它将帮助您自动为添加的表构建 Node.js 后端。您可以利用 Azure 门户上移动应用下的“开发工具 > 应用服务编辑器(预览版)”来检查您的后端。

Is it by design that all users, that are authenticated, see the data from all other users? So do I have to add some column with a user ID (based on the Microsoft Account ID) and add a condition to the where clause when getting data?

对于移动客户端,您可以使用 Azure Mobile Client SDK用于连接您的 azure 移动应用程序。假设您利用Server-managed authentication并使用以下代码通过 Microsoft 帐户进行登录:

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

您可以通过 MobileServiceClient.CurrentUser 检索记录的用户信息,并且 CurrentUser 具有属性 UserId。由于您使用的是 Easy Table,因此您只需通过 Azure 门户将 UserId 列添加到每个与用户相关的表中即可。此外,您需要在移动客户端应用程序中更改相关的SQLite表。

对于离线同步,您可以按如下方式定义 OData 查询,以从远程表中检索属于指定用户的项目。

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

更多详细信息,我建议您引用adrian Hall的书Data Access and Offline Sync .

更新:

如有疑问:

1) 在后端评估过滤条件,并且仅同步具有给定 userId 的行,您可以利用 Fiddler用于在处理拉操作时捕获网络跟踪。

2) 为了获得更安全的方法,您可以在将数据存储在服务器端之前调整发送到表 Controller 的数据,而不是指定 userId在您的移动客户端上。对于node.js后端,您可以在插入数据之前修改代码如下:

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

更多详情,您可以引用30 DAYS OF ZUMO.V2 (AZURE MOBILE APPS): DAY 6 – PERSONAL TABLES .

此外,对于 C# 后端,您可以按如下方式检索 UserId:

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

并修改添加新项目的操作,如下所示:

// 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);
}

更多详情,您可以引用Per-User Data .

关于c# - Azure移动应用程序离线同步-不同用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180901/

相关文章:

c# - VSTO Word 加载项 user.config 每次更新都丢失

c# - Selenium 不进入网站

c# - HiddenField 不是 WebControl?

c# - 问题与 Task.WhenAll 并行调用 Azure 移动客户端

c# - 将 CDATA 附加到字符串

Azure B2C 错误 : Application faulted after creating new app in MSAL portal

c# - 如何在 cosmos db C# 中插入一个文档的项目列表?

启用 MFA 的 Azure AD B2C 注册-登录策略 - 自定义登录页面

c# - 调试Xamarin iOS System.Net.HttpMessageHandler时出错

javascript - Azure 移动服务 - 从两个表读取并返回自定义响应对象