private static ServerCommandServiceClient CreateWcfClient(LoginSettings loginSettings, Uri uri) { var binding = GetBinding(loginSettings.IsBasicUser); var spn = SpnFactory.GetSpn(uri); var endpoint = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity(spn)); var client = new ServerCommandServiceClient(binding, endpoint); if (loginSettings.IsBasicUser) { client.ClientCredentials.UserName.UserName = "******" + loginSettings.NetworkCredential.UserName; client.ClientCredentials.UserName.Password = loginSettings.NetworkCredential.Password; // If it's basic user, you need to specify the certificate validation mode yourself client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None }; } else { client.ClientCredentials.Windows.ClientCredential = LoginSettingsCache.GetNetworkCredential(EnvironmentManager.Instance.MasterSite.ServerId); } return(client); }
/// <summary> /// Constructor for the BasicConnection, which performs and sets some start-up routines /// </summary> /// <param name="isCServer">If the server is a C server</param> /// <param name="username">Username to use</param> /// <param name="password">Password to use</param> /// <param name="hostname">The hostname</param> /// <param name="port">Which port to use</param> public BasicConnection(bool isCServer, String username, String password, String hostname, int port) { //Precondition if (hostname.StartsWith("http://")) { hostname = hostname.Substring("http://".Length); } IsCServer = isCServer; _serverUrl = hostname; _username = username; _password = password; _port = port; if (IsCServer) { // SSL _uri = new Uri($"https://{_serverUrl}:{_port}/ManagementServer/ServerCommandService.svc"); // Create Soap class from interface CServer = new ServerCommandServiceClient(GetBinding(), new EndpointAddress(_uri, EndpointIdentity.CreateSpnIdentity(SpnFactory.GetSpn(_uri)))); // Set basic credentials CServer.ClientCredentials.UserName.UserName = username; CServer.ClientCredentials.UserName.Password = password; // TODO Any certificate is accepted as OK !! CServer.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None, }; // Open service (internally) CServer.Open(); // Give OS a few milliseconds to get ready long tickStop = DateTime.Now.Ticks + 1000000; // 1.000.000 ticks == 100 ms while (DateTime.Now.Ticks < tickStop && CServer.State == CommunicationState.Opening) { Thread.Sleep(5); } } else { _eServer = new ServerCommandService($"http://{_serverUrl}:{_port}/ServerCommandService/servercommandservice.asmx"); String uri = $"http://{_serverUrl}"; _uri = new Uri(uri); CredentialCache credCache = new CredentialCache(); NetworkCredential credentials = new NetworkCredential(_username, _password); credCache.Add(new Uri(uri), "Basic", credentials); _eServer.Credentials = credCache; } }
/// <summary> /// Constructor of the NtlmConnection class. Will perform and set the needed start-up routines /// </summary> /// <param name="domain">The domain (may be empty)</param> /// <param name="authType">Authentication type</param> /// <param name="username">The username</param> /// <param name="password">The password related to the username</param> /// <param name="hostname">The hostname</param> /// <param name="port">The used port</param> public NtlmConnection(String domain, AuthenticationType authType, String username, String password, String hostname, int port = 80) { //Precondition if (hostname.StartsWith("http://")) { hostname = hostname.Substring("http://".Length); } _serverUrl = hostname; _port = port; _authType = authType; _username = username; _password = password; _domain = domain; String url; String prefix = "http"; if (_port == 443) { prefix += "s"; } url = $"{prefix}://{hostname}:{_port}/ManagementServer/ServerCommandService.svc"; WSHttpBinding binding = new WSHttpBinding() { MaxReceivedMessageSize = 1000000 }; EndpointAddress remoteAddress = new EndpointAddress(url); Server = new ServerCommandServiceClient(binding, remoteAddress); Server.ClientCredentials.Windows.ClientCredential.UserName = username; Server.ClientCredentials.Windows.ClientCredential.Password = password; if (!String.IsNullOrEmpty(_domain)) { Server.ClientCredentials.Windows.ClientCredential.Domain = _domain; } // TODO Any certificate is accepted as OK !! Server.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None, }; }
/// <summary> /// Constructor for the BasicConnection, which performs and sets some start-up routines /// </summary> /// <param name="username">Username to use</param> /// <param name="password">Password to use</param> /// <param name="hostname">The hostname</param> /// <param name="port">Which port to use</param> public BasicConnection(String username, String password, String hostname, int port) { //Precondition if (hostname.StartsWith("http://")) { hostname = hostname.Substring("http://".Length); } _serverUrl = hostname; _username = username; _password = password; _port = port; // SSL _uri = new Uri($"https://{_serverUrl}:{_port}/ManagementServer/ServerCommandService.svc"); // Create Soap class from interface Server = new ServerCommandServiceClient(GetBinding(), new EndpointAddress(_uri, EndpointIdentity.CreateSpnIdentity(SpnFactory.GetSpn(_uri)))); // Set basic credentials Server.ClientCredentials.UserName.UserName = username; Server.ClientCredentials.UserName.Password = password; // TODO Any certificate is accepted as OK !! Server.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None, }; // Open service (internally) Server.Open(); // Give OS a few milliseconds to get ready long tickStop = DateTime.Now.Ticks + 1000000; // 1.000.000 ticks == 100 ms while (DateTime.Now.Ticks < tickStop && Server.State == CommunicationState.Opening) { Thread.Sleep(5); } }
static void Main(string[] args) { /************************************************************ * Please change these accordingly to your own environment * ************************************************************/ String username = ""; String password = ""; String domain = ""; AuthenticationType authType = AuthenticationType.WindowsDefault; String hostAddress = "localhost"; int port = 80; if (authType == AuthenticationType.Basic) { port = 443; //SSL } /************************************************************ * Beginning of program * ************************************************************/ Console.WriteLine("Milestone SDK Bookmarks demo (XProtect Corporate only)"); Console.WriteLine("Creates 2 new bookmarks and retrieves them using "); Console.WriteLine(" 1) BookmarkSearchTime "); Console.WriteLine(" 2) BookmarkSearchFromBookmark"); Console.WriteLine(" 3) BookmarkGet"); Console.WriteLine(" 4) BookmarkDelete"); Console.WriteLine(""); #region Connect to the Management Server, get configuration, and extract the cameras RecorderInfo[] recorderInfo = new RecorderInfo[0]; LoginInfo loginInfo = null; ServerCommandServiceClient scs = null; switch (authType) { case AuthenticationType.Basic: _basicConnection = new BasicConnection(username, password, hostAddress, port); loginInfo = _basicConnection.Login(IntegrationId, Version, IntegrationName); _basicConnection.GetConfiguration(loginInfo.Token); ConfigurationInfo confInfoBasic = _basicConnection.ConfigurationInfo; recorderInfo = confInfoBasic.Recorders; scs = _basicConnection.Server; break; case AuthenticationType.Windows: case AuthenticationType.WindowsDefault: _ntlmConnection = new NtlmConnection(domain, authType, username, password, hostAddress, port); loginInfo = _ntlmConnection.Login(IntegrationId, Version, IntegrationName); _ntlmConnection.GetConfiguration(loginInfo.Token); ConfigurationInfo confInfoNtlm = _ntlmConnection.ConfigurationInfo; recorderInfo = confInfoNtlm.Recorders; scs = _ntlmConnection.Server; break; default: //Empty break; } #endregion #region Find recording servers attached to the management server //Get recording servers int recorders = recorderInfo.Length; Console.WriteLine("{0} Corporate Recording Server found", recorders); if (recorders == 0) { Console.WriteLine(""); Console.WriteLine("Press any key"); Console.ReadKey(); return; } #endregion DateTime timeNow = DateTime.Now; // get cameras for the first recorder RecorderInfo recorder = recorderInfo[0]; Console.WriteLine(""); Console.WriteLine("Processing recording server {0}", recorder.Name); Console.WriteLine(""); #region Find all cameras defined on the recording server // extract info about the recording server List <CameraInfo> cameras = recorder.Cameras.ToList(); #endregion // now-5:10min: BookmarkSearchTime start // now-5:00min: (beginTime) | BookmarkGet // now-4:59min: start recording 1 | // now-4:55min: start bookmark 1 | // now-4:45min: end bookmark 1 | // | BookmarkSearchFromBookmark // now-2:00min: | | // now-1:59min: start recording 2 | | // now-1:55min: start bookmark 2 (trigger time) | | // now-1:45min: end bookmark 2 | | // now v V #region create first bookmark Guid cameraGuid = cameras.First().DeviceId; Console.WriteLine("Creating the first bookmark"); MediaDeviceType[] mediaDeviceTypes = new MediaDeviceType[3]; mediaDeviceTypes[0] = MediaDeviceType.Camera; mediaDeviceTypes[1] = MediaDeviceType.Microphone; mediaDeviceTypes[2] = MediaDeviceType.Speaker; DateTime timeBegin = timeNow.AddMinutes(-5); TimeDuration td = new TimeDuration() { MicroSeconds = (int)TimeSpan.FromMinutes(30).TotalMilliseconds * 1000 }; StringBuilder bookmarkRef = new StringBuilder(); StringBuilder bookmarkHeader = new StringBuilder(); StringBuilder bookmarkDesc = new StringBuilder(); bookmarkRef.AppendFormat("MyBookmark-{0}", timeBegin.ToLongTimeString()); bookmarkHeader.AppendFormat("AutoBookmark-{0}", timeBegin.ToLongTimeString()); bookmarkDesc.AppendFormat("AutoBookmark-{0} set for a duration of {1} seconds", timeBegin.ToLongTimeString(), (timeBegin.AddSeconds(10) - timeBegin.AddSeconds(1)).Seconds); Bookmark newBookmark = null; try { newBookmark = scs.BookmarkCreate(loginInfo.Token, cameraGuid, timeBegin.AddSeconds(1), timeBegin.AddSeconds(5), timeBegin.AddSeconds(10), bookmarkRef.ToString(), bookmarkHeader.ToString(), bookmarkDesc.ToString()); } catch (Exception ex) { Console.WriteLine("BookmarkCreate: " + ex.Message); Console.WriteLine("Press any Key to exit..."); Console.ReadKey(); Environment.Exit(0); } if (newBookmark == null) { Console.WriteLine("New bookmark wasn't created."); Console.WriteLine("Press any key to exit."); Console.ReadKey(); Environment.Exit(0); } Console.WriteLine("-> trigger time = {0}", newBookmark.TimeTrigged); Console.WriteLine(""); #endregion Console.WriteLine(""); Console.WriteLine("Waiting 20 sec ...."); Console.WriteLine(""); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(20)); #region Create a second bookmark Console.WriteLine("Creating a second bookmark - 2 minutes after the first bookmark"); DateTime timeBegin2 = timeBegin.AddMinutes(2); bookmarkHeader.Length = 0; bookmarkDesc.Length = 0; StringBuilder bookmarkRef2 = new StringBuilder(); bookmarkRef2.AppendFormat("MyBookmark-{0}", timeBegin2.ToLongTimeString()); bookmarkHeader.AppendFormat("AutoBookmark-{0}", timeBegin2.ToLongTimeString()); bookmarkDesc.AppendFormat("AutoBookmark-{0} set for a duration of {1} seconds", timeBegin2.ToLongTimeString(), (timeBegin2.AddSeconds(10) - timeBegin2.AddSeconds(1)).Seconds); Bookmark newBookmark2 = scs.BookmarkCreate(loginInfo.Token, cameraGuid, timeBegin2.AddSeconds(1), timeBegin2.AddSeconds(5), timeBegin2.AddSeconds(10) , bookmarkRef2.ToString(), bookmarkHeader.ToString(), bookmarkDesc.ToString()); Console.WriteLine("-> trigger time = {0}", newBookmark2.TimeTrigged); Console.WriteLine(""); #endregion #region BookmarkSearchTime // Get max 10 of the bookmarks created after the specified time Console.WriteLine(""); Console.WriteLine("Looking for bookmarks using BookmarkSearchTime (finding the 2 newly created)"); Bookmark[] bookmarkList = scs.BookmarkSearchTime(loginInfo.Token, newBookmark.TimeBegin.AddSeconds(-10), td, 10, mediaDeviceTypes, new Guid[0], new string[0], ""); if (bookmarkList.Length > 0) { Console.WriteLine("-> Found {0} bookmark(s)", bookmarkList.Length); int counter = 1; foreach (Bookmark bookmark in bookmarkList) { Console.WriteLine("{0}:", counter); Console.WriteLine(" Id ={0} ", bookmark.Id); Console.WriteLine(" Name={0} ", bookmark.Header); Console.WriteLine(" Desc={0} ", bookmark.Description); Console.WriteLine(" user={0} ", bookmark.User); Console.WriteLine(" Device={0} Start={1} Stop={2} ", bookmark.DeviceId, bookmark.TimeBegin, bookmark.TimeEnd); counter++; } } else { Console.WriteLine("sorry no bookmarks found"); } Console.WriteLine(""); #endregion #region BookmarkSearchFromBookmark // Get the next (max 10) bookmarks after the first Console.WriteLine( "Looking for bookmarks using BookmarkSearchFromBookmark (finding the last of the 2 newly created)"); Bookmark[] bookmarkListsFromBookmark = scs.BookmarkSearchFromBookmark(loginInfo.Token, newBookmark.Id, td, 10, mediaDeviceTypes, new Guid[0], new string[0], ""); if (bookmarkListsFromBookmark.Length > 0) { Console.WriteLine("-> Found {0} bookmark(s)", bookmarkListsFromBookmark.Length); int counter = 1; foreach (Bookmark bookmark in bookmarkListsFromBookmark) { Console.WriteLine("{0}:", counter); Console.WriteLine(" Id ={0} ", bookmark.Id); Console.WriteLine(" Name={0} ", bookmark.Header); Console.WriteLine(" Desc={0} ", bookmark.Description); Console.WriteLine(" user={0} ", bookmark.User); Console.WriteLine(" Device={0} Start={1} Stop={2} ", bookmark.DeviceId, bookmark.TimeBegin, bookmark.TimeEnd); counter++; } } else { Console.WriteLine("sorry no bookmarks found"); } Console.WriteLine(""); #endregion #region BookmarkGet // Get first created bookmark Console.WriteLine( "Looking for the first bookmarks using BookmarkGet (finding the first of the 2 newly created)"); Bookmark newBookmarkFetched = scs.BookmarkGet(loginInfo.Token, newBookmark.Id); if (newBookmarkFetched != null) { Console.WriteLine("-> A bookmarks is found"); Console.WriteLine(" Id ={0} ", newBookmarkFetched.Id); Console.WriteLine(" Name={0} ", newBookmarkFetched.Header); Console.WriteLine(" Desc={0} ", newBookmarkFetched.Description); Console.WriteLine(" user={0} ", newBookmarkFetched.User); Console.WriteLine(" Device={0} Start={1} Stop={2} ", newBookmarkFetched.DeviceId, newBookmarkFetched.TimeBegin, newBookmarkFetched.TimeEnd); } else { Console.WriteLine("Sorry no bookmarks found"); } Console.WriteLine(""); #endregion #region Deleting bookmarks Console.WriteLine("Deleting 2 newly created bookmarks"); scs.BookmarkDelete(loginInfo.Token, newBookmark.Id); Console.WriteLine(" -> first deleted"); scs.BookmarkDelete(loginInfo.Token, newBookmark2.Id); Console.WriteLine(" -> second deleted"); #endregion Console.WriteLine(""); Console.WriteLine("Press any key"); Console.ReadKey(); }
/// <summary> /// Constructor of the NtlmConnection class. Will perform and set the needed start-up routines /// </summary> /// <param name="domain">The domain (may be empty)</param> /// <param name="authType">Authentication type</param> /// <param name="username">The username</param> /// <param name="password">The password related to the username</param> /// <param name="isCCode">If the server is a C server</param> /// <param name="hostname">The hostname</param> /// <param name="port">The used port</param> public NtlmConnection(String domain, AuthenticationType authType, String username, String password, bool isCCode, String hostname, int port = 80) { //Precondition if (hostname.StartsWith("http://")) { hostname = hostname.Substring("http://".Length); } _serverUrl = hostname; _port = port; _authType = authType; _username = username; _password = password; _domain = domain; IsCCode = isCCode; String url; String prefix = "http"; if (_port == 443) //Note: E servers doesn't support SSL { prefix += "s"; } if (IsCCode) { url = $"{prefix}://{hostname}:{_port}/ManagementServer/ServerCommandService.svc"; WSHttpBinding binding = new WSHttpBinding() { MaxReceivedMessageSize = 1000000 }; EndpointAddress remoteAddress = new EndpointAddress(url); CServer = new ServerCommandServiceClient(binding, remoteAddress); CServer.ClientCredentials.Windows.ClientCredential.UserName = username; CServer.ClientCredentials.Windows.ClientCredential.Password = password; if (!String.IsNullOrEmpty(_domain)) { CServer.ClientCredentials.Windows.ClientCredential.Domain = _domain; } // TODO Any certificate is accepted as OK !! CServer.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication() { CertificateValidationMode = X509CertificateValidationMode.None, }; } else { url = $"{prefix}://{hostname}"; _eServer = new ServerCommandService($"http://{_serverUrl}/ServerCommandService/servercommandservice.asmx"); CredentialCache credCache = new CredentialCache(); NetworkCredential credentials; if (String.IsNullOrEmpty(_domain)) { credentials = new NetworkCredential(_username, _password); } else { credentials = new NetworkCredential(_username, _password, _domain); } credCache.Add(new Uri(url), "NTLM", credentials); _eServer.Credentials = credCache; } }