2016-07-17 6 views
0

私はDB 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)です。

次の同期Gatewayの設定を持っていると仮定すると

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

あなたが各レプリケーションにLastErrorプロパティをチェックするStartReplicationWithAuth方法で次のように追加することができます。

 ... 
     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. 
     } 
    } 

のNb:Iドン」 couchbase-lite-net SDKがステータスコードをアプリケーションに公開しているかどうかを確認します(401はクレデンシャルが正しくないことを示し、404はリモートURLが見つからなかったことを示します)。出願番号https://github.com/couchbase/couchbase-lite-net/issues/695

+0

ありがとうございました 上記のコードは、私が探していたものです。 – RRH

関連する問題