public void Connect(Uri uri)
 {
     _uri = uri;
     try
     {
         Environment.AddServer(uri, CredentialCache.DefaultNetworkCredentials);
         Environment.Login(uri);
         _configService = new XProtectConfigurationService(uri);
     }
     catch (ServerNotFoundMIPException)
     {
         LastError = $"Login to {_uri} failed. Server not found.";
     }
     catch (InvalidCredentialsMIPException ex)
     {
         LastError = $"Login to {_uri} failed. {ex.Message}";
     }
     catch (LoginFailedInternalMIPException ex)
     {
         LastError = $"Login to {_uri} failed. {ex.Message}";
     }
     catch (Exception e)
     {
         LastError = e.ToString();
     }
 }
예제 #2
0
        private static void PrepareToLogin(string serverName, string userName, string password)
        {
            ServerName        = serverName;
            milestoneUserName = userName;
            milestonePassword = password;

            if (string.IsNullOrEmpty(ServerName))
            {
                string msg = "Server address cannot be emnpty";
                Program.Log.Warn(msg);
                Console.WriteLine(msg);
                return;
            }

            Uri               uri    = null;
            CredentialCache   cc     = null;
            NetworkCredential nc     = null;
            bool              logged = false;

            if (!string.IsNullOrEmpty(milestoneUserName))
            {
                // You need this to apply Enterprise "basic" credentials.
                uri = new UriBuilder(ServerName).Uri;
                cc  = Util.BuildCredentialCache(uri, milestoneUserName, milestonePassword, "Negotiate");//"Basic" : "Negotiate"
                SDKEnvironment.AddServer(uri, cc);
                // use the same credentiel as when logge on to the Management Server
                logged = Login(uri);
            }

            if (!logged)
            {
                try
                {
                    uri = new UriBuilder(ServerName).Uri;
                }
                catch (Exception ex)
                {
                    Program.Log.Error(ServerName, ex);
                    Program.Log.Error(ServerName, ex.InnerException);
                    return;
                }

                // This will reuse the Windows credentials you are logged in with
                nc = CredentialCache.DefaultNetworkCredentials;
                SDKEnvironment.AddServer(uri, nc);

                //Pick up the login settings from the management server to be used when logging into the Recording Server.
                loginSettings = LoginSettingsCache.GetLoginSettings(ServerName);

                logged = Login(uri);
            }

            if (!logged)
            {
                uri = new Uri($"http://{Configuration.Instance.ServerFQID.ServerId.ServerHostname}:{Configuration.Instance.ServerFQID.ServerId.Serverport}/");
                SDKEnvironment.AddServer(uri, nc);
                logged = Login(uri);
            }

            if (!logged && loginSettings != null)
            {
                SDKEnvironment.AddServer(uri, nc);
                logged = Login(loginSettings.Uri);
            }

            if (!logged)
            {
                Program.Log.Error("Doesn't logged in.");
                return;
            }

            try
            {
                if (!SDKEnvironment.IsServerConnected(uri))
                {
                    string msg = $"Failed to connect to {uri}";
                    Program.Log.Warn(msg);
                    return;
                }

                scs          = new ServerCommandService();
                scs.Timeout *= 3;                                       // The default is 100,000 milliseconds.

                // use the same credentiel as when logge on to the Management Server
                if (cc != null)
                {
                    scs.Credentials = cc;
                }
                else if (nc != null)
                {
                    scs.Credentials = nc;
                }

                connectedToMilestone = true;
                List <Item> items = Configuration.Instance.GetItems();
                EnumerateElementChildren(items, AllCameras);
                ProductCode = EnvironmentManager.Instance.SystemLicense.ProductCode;
                ServerId    = Configuration.Instance.ServerFQID.ServerId.Id;
            }
            catch (Exception ex)
            {
                Program.Log.Error($"Internal error connecting to: {uri.DnsSafeHost}", ex);
            }

            if (loginSettings != null)
            {
                Token = loginSettings.Token;
            }

            if (connectedToMilestone)
            {
                try
                {
                    string           path             = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    MilestoneVersion milestoneVersion = new MilestoneVersion();
                    milestoneVersion.Connection = connectedToMilestone;

                    File.WriteAllText($"{path}\\Milestone.json", JsonHelper.FromClass(milestoneVersion));
                }
                catch (Exception ex)
                {
                    Program.Log.Error(null, ex);
                }
                scs.Url = $"{ServerName}ServerAPI/ServerCommandService.asmx";
                string msg = $"Connected to Milestone server: {ServerName}";

                MilestoneInfo();

                Console.WriteLine(msg);
                Program.Log.Info(msg);
            }
            else
            {
                Console.WriteLine($"Failed to connect to {ServerName}");
            }
        }