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");
}
}
}
謝謝 上面提供的代碼正是我一直在尋找的。 – RRH