2016-07-17 27 views
0

我正在研究一个应用程序,它需要用户输入数据库I.P.并在第一次运行应用程序时移植。我创建了一个名为Database.cs(后面的代码)的类,它处理身份验证。如果输入正确的信息,一切正常;但是,我无法验证是否添加了不正确的信息。我知道我可以对它进行硬编码,但我正在寻找一种方法来处理它,而不是采用硬编码。Xamarin Visual Studio Android C#如何验证couchbase lite凭证

Database.cs

using System; 
    using Android.Util; 

    using Couchbase.Lite; 
    using Couchbase.Lite.Auth; 

    namespace Core 
    { 
    public class Database 
    { 
     static readonly string TAG = "eTest: " + typeof(Database).Name; 
     const string DB_NAME = "dev"; 

     public static Couchbase.Lite.Database db; 

     static public Replication pull; 
     static public Replication push; 


     public static void CreateDatabase() 
     { 
      try 
      { 
       Log.Debug(TAG, "Entering method CreateDatabase try block"); 
       db = Manager.SharedInstance.GetDatabase(DB_NAME); 
       Log.Debug(TAG, "Exiting method CreateDatabase try block"); 
      } 
      catch (Exception e) 
      { 
       Log.Debug(TAG, "Entering method CreateDatabase catch block"); 
       Log.Error(TAG, "Error getting database", e); 
       Log.Debug(TAG, "Exiting method CreateDatabase catch block"); 
       return; 
      } 
     } 

     public static Uri CreateSyncUri(string host, int port) 
     { 
      Log.Debug(TAG, "Entering method CreateSyncUri"); 
      Uri syncUri = null; 
      string scheme = "http"; 
      try 
      { 
       Log.Debug(TAG, "Entering try block of method CreateSyncUri"); 
       var uriBuilder = new UriBuilder(scheme, host, port, DB_NAME); 
       syncUri = uriBuilder.Uri; 
       Log.Debug(TAG, "Exiting try block of method CreateSyncUri"); 
      } 
      catch (UriFormatException e) 
      { 
       Log.Error(TAG, " Cannot Create sync uri", e); 
      } 

      return syncUri; 
     } 

     public static void StartReplicationWithAuth(string host, int port) 
     { 
      Log.Debug(TAG, "Entering method StartReplicationWithAuth"); 
      pull = db.CreatePullReplication(CreateSyncUri(host, port)); 
      push = db.CreatePushReplication(CreateSyncUri(host, port)); 
      var authenticator = AuthenticatorFactory.CreateBasicAuthenticator("name", "password"); 
      pull.Authenticator = authenticator; 
      push.Authenticator = authenticator; 
      pull.Continuous = true; 
      push.Continuous = true; 
      pull.Start(); 
      push.Start(); 
      Log.Debug(TAG, "Exiting method StartReplicationWithAuth"); 
     } 
    } 
} 

回答

0

在这种情况下,用户始终是相同的(用户名:name,密码:password)。

假设你有以下同步网关配置:

{ 
    "databases": { 
     "dev": { 
      "bucket": "walrus", 
      "users": {"name": {"password": "password", "admin_channels": ["*"]}} 
     } 
    } 
} 

您可以添加在StartReplicationWithAuth方法下面来检查每个复制LastError属性:

 ... 
     pull.Changed += Changed; 
     push.Changed += Changed; 
    } 

    void Changed(object sender, ReplicationChangeEventArgs e) 
    { 
     if (pull.Status == ReplicationStatus.Active || push.Status == ReplicationStatus.Active) 
     { 
      Console.WriteLine($"Sync in progress"); 
     } 
     else if (pull.LastError != null || push.LastError != null) 
     { 
      Exception error = pull.LastError != null ? pull.LastError : push.LastError; 
      Console.WriteLine($"Error message :: {error}"); 
      // Error in replication. 
     } 
    } 

注:我不t知道couchbase-lite-net SDK是否向应用程序公开状态码(401表示凭证不正确,404表示远程URL未找到)。提交https://github.com/couchbase/couchbase-lite-net/issues/695

+0

谢谢 上面提供的代码正是我一直在寻找的。 – RRH