2016-07-17 53 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