【问题标题】:Xamarin forms and Firebase: read rulesXamarin 表单和 Firebase:阅读规则
【发布时间】:2020-07-29 13:44:57
【问题描述】:

我无法理解和配置 Firebase 云 Firestone 安全规则。 我正在使用 Plugin.CloudFirestore。

完全释放安全性后,我可以访问。见规则和代码:

match /{document=**} {
    allow read: if true;
    allow write: if true
}

 public async Task<IQuerySnapshot> GetCollection(string collection)
{
    var group = await CrossCloudFirestore.Current
                             .Instance
                             .GetCollectionGroup(collection)
                             .GetDocumentsAsync();

    return group;
}

现在,由于不允许以下规则,访问被拒绝:

 match /Categorias/{document=**} {
    allow read: if true;
    allow write: if true
 }

like this: match / Categorias / {document = **} - 我只能通过指定文档获取数据,无法获取文档列表。

请帮助我了解我缺少什么。

【问题讨论】:

  • 如果你这样做.GetCollection("Categorias") 会发生什么,所以使用硬编码的名称并且只获取单个集合而不是组?
  • 嗨弗兰克,规则:match /Categorias/{document=**} { allow read: if true; allow write: if true } 如果我指定文档,它可以工作:firebase.GetDocument("Categorias", "3"); 如果我必须返回集合中的所有文档:firebase.GetCollection("Categorias") 发生错误:PERMISSION_DENIED: Missing or权限不足
  • 如果你改成match /Categorias/{document}这样的东西,能用吗?除此之外,您能否检查一下您的服务帐户是否具有完全访问权限,如 here 所阐明的那样?也许您正在使用它,但它没有所有需要的权限。
  • 嗨 gso_gabriel 我检查了match /Categorias/{document} 并得到了相同的结果。关于服务帐户,不是这样,因为如果我配置规则 match /{document=**} { allow read: if true; allow write: if true } 我可以让 GetCollection 工作。
  • 嗨@JoséDonizeteOliveiraJunior 考虑到这一点,我建议您直接联系Firebase Support,以便他们帮助您检查。您的规则以及您的帐户权限似乎都是正确的,因此他们可以进一步调查您的实例。

标签: c# xamarin google-cloud-firestore firebase-security


【解决方案1】:

Firebase 支持:

我已经检查了您的情况,并且您使用的是递归通配符,其行为取决于规则版本,我可以看到您使用的是 rules_version = '2',而且您使用的是集合组查询,因为您使用 GetCollectionGroup 而不是 GetCollection .在使用集合组查询之前,您必须创建一个支持您的集合组查询的索引。您可以通过错误消息、控制台或 Firebase CLI 创建索引。您还必须创建允许您的集合组查询的规则。您的安全规则可能是:

rules_version = '2';

服务 cloud.firestore {

匹配 /databases/{database}/documents {

// Authenticated users can query the Categorias collection group

// Applies to collection Categorias and

// single document retrievals

match /{document=**} {

  allow read: if request.auth != null; //These conditions could change 

}

match /Categorias/{document=**} {

  // Only a post's author can write to a post

  allow write: if request.auth != null && request.auth.uid == resource.data.author; //These conditions could change



}

}

}

我不知道您是否出于任何特定原因使用递归通配符,但是检查您的数据结构,您可以使用版本 1 和简单的通配符来简化您的安全规则。另外,您应该使用 GetCollection 来获取集合。

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多