繁体   English   中英

如何使用ONVIF对AXIS摄像机进行身份验证

[英]How to Authenticate an AXIS camera with ONVIF

我有3个ONVIF相机(Bosch,Pansonic和AXIS)。 我使用WS-Discovery查找摄像机,并且可以使用GetDeviceInformation从摄像机获取信息。 我的问题是,当我尝试从中获取信息时,AXIS相机返回(400)错误的请求,另外两个则像超级按钮一样工作。

我已经从SourceForge安装了ONVIF设备管理器。 如果我在程序中输入登录凭据,则可以从AXIS摄像机流式传输实时视频。 如果我没有输入任何登录凭据,则可以找到相机,但不能流式传输任何视频。 因此,基于此我得出结论,相机配置正确。

我认为这与绑定上的登录凭证有关,但无法弄清楚出了什么问题。

我的代码看起来像这样

private void CustomBinding2()
{
        try
        {
            const string SERVICE_ADDRESS_DIRECT = "http://192.168.1.72/onvif/device_service"; //400 bad request
            const string USERNAME = "cbk";
            const string PASSWORD = "12";

            HttpTransportBindingElement httpTransportBindingElement = new HttpTransportBindingElement();
            httpTransportBindingElement.MaxReceivedMessageSize = Int32.MaxValue;
            httpTransportBindingElement.KeepAliveEnabled = false;
            httpTransportBindingElement.MaxBufferSize = Int32.MaxValue;
            httpTransportBindingElement.ProxyAddress = null;
            httpTransportBindingElement.BypassProxyOnLocal = true;
            httpTransportBindingElement.UseDefaultWebProxy = false;
            httpTransportBindingElement.TransferMode = TransferMode.StreamedResponse;
            httpTransportBindingElement.AuthenticationScheme = AuthenticationSchemes.Basic;

            TextMessageEncodingBindingElement messegeElement = new TextMessageEncodingBindingElement();
            messegeElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);

            CustomBinding binding = new CustomBinding(messegeElement, httpTransportBindingElement);
            binding.CloseTimeout = TimeSpan.FromSeconds(30.0);
            binding.OpenTimeout = TimeSpan.FromSeconds(30.0);
            binding.SendTimeout = TimeSpan.FromMinutes(10.0);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(3.0);

            EndpointAddress serviceAddress = new EndpointAddress(SERVICE_ADDRESS_DIRECT);

            ChannelFactory<Device> channelFactory = new ChannelFactory<Device>(binding, serviceAddress);
            channelFactory.Credentials.UserName.UserName = USERNAME;
            channelFactory.Credentials.UserName.Password = PASSWORD;

            Device channel = channelFactory.CreateChannel();

            string model, firmwareVersion, serialNumber, hardwareId;
            channel.GetDeviceInformation(out model, out firmwareVersion, out serialNumber, out hardwareId);
            MessageBox.Show(string.Format("Model: {0}", model));
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
}

解决了问题...

最让我受骗的一件事是,AXIS摄像机和客户端(PC)必须在+ -5秒内保持时间同步。 如果更改PC时间,我只会收到400错误的请求。 如果时间匹配,一切正常!!!

            DateTime UTCTime = DateTime.UtcNow;

            tbInfo.AppendText(string.Format("Client Local Time: {0}\n", DateTime.Now.ToString("HH:mm:ss")));
            tbInfo.AppendText(string.Format("Client UTC Time: {0}\n", UTCTime.ToString("HH:mm:ss")));
            tbInfo.AppendText("\n\n");

            HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();

            TransportSecurityBindingElement transportSecurity = new TransportSecurityBindingElement();
            transportSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UsernameTokenParameters());
            transportSecurity.AllowInsecureTransport = true;
            transportSecurity.IncludeTimestamp = false;

            TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8);

            CustomBinding binding = new CustomBinding(transportSecurity, textMessageEncoding, httpTransport);

            EndpointAddress serviceAddress = new EndpointAddress(addressDirect);
            ChannelFactory<Device> channelFactory = new ChannelFactory<Device>(binding, serviceAddress);

            UsernameClientCredentials credentials = new UsernameClientCredentials(new UsernameInfo(username, password));

            channelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
            channelFactory.Endpoint.Behaviors.Add(credentials);

            Device channel = channelFactory.CreateChannel();

            var unitTime = channel.GetSystemDateAndTime(new GetSystemDateAndTimeRequest());
            tbInfo.AppendText(string.Format("Camera Local Time: {0}:{1}:{2}\n", unitTime.SystemDateAndTime.LocalDateTime.Time.Hour, unitTime.SystemDateAndTime.LocalDateTime.Time.Minute, unitTime.SystemDateAndTime.LocalDateTime.Time.Second));
            tbInfo.AppendText(string.Format("Camera UTC Time: {0}:{1}:{2}\n", unitTime.SystemDateAndTime.UTCDateTime.Time.Hour, unitTime.SystemDateAndTime.UTCDateTime.Time.Minute, unitTime.SystemDateAndTime.UTCDateTime.Time.Second));

            var info = channel.GetDeviceInformation(new GetDeviceInformationRequest());
            MessageBox.Show(string.Format("Model: {0}", info.Model));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM