【问题标题】:How to create user in couchbase from Xamarin application?如何从 Xamarin 应用程序在 couchbase 中创建用户?
【发布时间】:2019-12-24 09:21:15
【问题描述】:

我正在创建一个使用 couchbase lite、同步网关和 couchbase 服务器的 xamarin 应用程序,

我遵循了这个教程:https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/xamarin/userprofile_sync.html (但我使用 server : 127.0.0.1 而不是 walrus )

这里是从同步网关的配置文件创建用户,以便能够在服务器上同步用户及其数据,但是我需要从应用程序本身而不是配置文件创建用户,我该怎么做请问这个?

这是我创建用户的配置文件:

    {
     "log": ["*","Debug"],
     "databases": {
     "userprofile": {
     "server": "http://127.0.0.1:8091",
     "bucket": "userprofile",
     "username": "Maria", 
     "password": "123456", 
     "enable_shared_bucket_access": true, 
     "import_docs": "continuous",
     "num_index_replicas": 0, 
     "delta_sync" :{"enabled":true},
      "users": {
      "Maria": { "password": "123456"},
      "Mina": { "password": "123456"},
      "GUEST": { "disabled": false, "admin_channels": ["*"] }
  },
      "sync": `
      function sync(doc, oldDoc) {

     /* Authorization */

     // Verify the user making the request is the same as the one in doc's email
     requireUser(doc.email);
     /* Data Validation */

  if (!isDelete()) {
  // Validate the presence of email fields
  validateNotEmpty("email", doc.email);

  // Check if document is being created / added for first time
  // We allow any user to create the document
  if (isCreate()) {

    // Validate that the document Id _id is prefixed by owner.
    var expectedDocId = "user" + "::" + doc.email;

    if (expectedDocId != doc._id) {
        throw({forbidden: "user doc Id must be of form user:email"});

    }

  } else {       

     // Validate that the email hasn't changed.
    validateReadOnly("email", doc.email, oldDoc.email);
  }

}


 /* Routing */
 // Subsequent updates to document must be authorized
 var email = getEmail();

 // Add doc to the user's channel.
 channel("channel." + email);

 /* Access Control */
 // Give user read access to channel
 if (!isDelete()) {
 // Deletion of user document is essentially deletion of user
   access(email,"channel." + email)
 }

 // get type property 
 function getType() {
 return (isDelete() ? oldDoc.type : doc.type);
 }

 // get email Id property
function getEmail() {
return (isDelete() ? oldDoc.email : doc.email);
 }

 // Check if document is being created/added for first time
 function isCreate() {
 // Checking false for the Admin UI to work
  return ((oldDoc == false) || (oldDoc == null || oldDoc._deleted) && !isDelete());
 }

 // Check if this is a document update
 function isUpdate() {
  return (!isCreate() && !isDelete());
}

// Check if this is a document delete
 function isDelete() {
  return (doc._deleted == true);
}

// Verify that specified property exists
function validateNotEmpty(key, value) {
  if (!value) {
    throw({forbidden: key + " is not provided."});
  }
}

// Verify that specified property value has not changed during update
function validateReadOnly(name, value, oldValue) {
  if (value != oldValue) {
    throw({forbidden: name + " is read-only."});
    }
  }
 }
   `
  }
 }
}

【问题讨论】:

    标签: c# authentication xamarin couchbase-lite couchbase-sync-gateway


    【解决方案1】:

    在上面的示例中,您必须创建一个名为 userprofile 用户名 ** Maria** 和密码 123456 的存储桶。以下是有关如何执行此操作的文档:

    https://docs.couchbase.com/server/6.0/manage/manage-security/manage-users-and-roles.html

    OBS:用户不必叫Maria,这只是Sync gateway用来连接couchbase的用户:

     "bucket": "userprofile",
     "username": "someuser", 
     "password": "somepassword"
    

    如果您使用密码 somepassword 创建了一个名为 someuser 的用户,则上面的 sn-p 是您必须在同步网关配置文件中指定的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多