【问题标题】:Xamarin Visual Studio Android C# How to validate couchbase lite credentialsXamarin Visual Studio Android C# 如何验证 couchbase lite 凭据
【发布时间】:2016-07-17 00:26:18
【问题描述】:

我正在开发一个应用程序,该应用程序需要用户输入数据库 IP。并在应用程序第一次运行时移植。我创建了一个名为 Database.cs 的类(后面的代码),它处理身份验证。如果输入正确的信息,一切正常;但是,我无法验证是否添加了不正确的信息。我知道我可以通过硬编码,但我正在寻找一种方法来处理它而不诉诸硬编码。

数据库.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");
        }
    }
}

【问题讨论】:

    标签: xamarin xamarin.android xamarin-studio couchbase-lite


    【解决方案1】:

    在这种情况下,用户总是相同的(用户名: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.
            }
        }
    

    N.b.:我不知道 couchbase-lite-net SDK 是否将状态代码暴露给应用程序(401 表示凭据不正确,404 表示未找到远程 URL)。归档https://github.com/couchbase/couchbase-lite-net/issues/695

    【讨论】:

    • 谢谢 上面提供的代码正是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2023-02-06
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多