Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ONVIF对AXIS相机进行身份验证_C#_Onvif - Fatal编程技术网

C# 如何使用ONVIF对AXIS相机进行身份验证

C# 如何使用ONVIF对AXIS相机进行身份验证,c#,onvif,C#,Onvif,我有3台ONVIF摄像机(博世、Pansonic和AXIS)。我使用WS-Discovery查找摄像头,并可以使用GetDeviceInformation从摄像头获取信息。我的问题是,当我试图从AXIS相机获取信息时,AXIS相机返回(400)错误的请求,而另外两个相机工作得很好 我已从SourceForge安装了ONVIF设备管理器。如果我在程序中输入登录凭据,我可以从AXIS摄像机流式传输实时视频。如果我没有输入任何登录凭据,我可以找到相机,但不能流式传输任何视频。基于此,我得出结论,相机配

我有3台ONVIF摄像机(博世、Pansonic和AXIS)。我使用WS-Discovery查找摄像头,并可以使用GetDeviceInformation从摄像头获取信息。我的问题是,当我试图从AXIS相机获取信息时,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);
        }
}
private void CustomBinding2()
{
尝试
{
常量字符串服务\u地址\u直接=”http://192.168.1.72/onvif/device_service“;//400错误的请求
常量字符串USERNAME=“cbk”;
const string PASSWORD=“12”;
HttpTransportBindingElement HttpTransportBindingElement=新的HttpTransportBindingElement();
httpTransportBindingElement.MaxReceivedMessageSize=Int32.MaxValue;
httpTransportBindingElement.KeepAliveEnabled=false;
httpTransportBindingElement.MaxBufferSize=Int32.MaxValue;
httpTransportBindingElement.ProxyAddress=null;
httpTransportBindingElement.BypassProxyOnLocal=true;
httpTransportBindingElement.UseDefaultWebProxy=false;
httpTransportBindingElement.TransferMode=TransferMode.StreamdResponse;
httpTransportBindingElement.AuthenticationScheme=AuthenticationSchemes.Basic;
TextMessageEncodingBindingElement消息元素=新的TextMessageEncodingBindingElement();
messegeElement.MessageVersion=MessageVersion.CreateVersion(EnveloperVersion.Soap12,AddressingVersion.None);
CustomBinding=新的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=新的EndpointAddress(服务地址直接);
ChannelFactory ChannelFactory=新的ChannelFactory(绑定,服务地址);
channelFactory.Credentials.UserName.UserName=用户名;
channelFactory.Credentials.UserName.Password=密码;
设备通道=channelFactory.CreateChannel();
字符串模型,固件版本,序列号,硬件ID;
获取设备信息(输出型号、输出固件版本、输出序列号、输出硬件ID);
Show(string.Format(“Model:{0}”,Model));
}
捕获(例外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));
DateTime UTCTime=DateTime.UtcNow;
tbInfo.AppendText(string.Format(“客户端本地时间:{0}\n”,DateTime.Now.ToString(“HH:mm:ss”));
tbInfo.AppendText(string.Format(“客户端UTC时间:{0}\n”,UTCTime.ToString(“HH:mm:ss”));
tbInfo.AppendText(“\n\n”);
HttpTransportBindingElement httpTransport=新的HttpTransportBindingElement();
TransportSecurityBindingElement transportSecurity=新的TransportSecurityBindingElement();
transportSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add(新用户名TokenParameters());
transportSecurity.AllowInsecureTransport=true;
transportSecurity.IncludeTimestamp=false;
TextMessageEncodingBindingElement textMessageEncoding=新的TextMessageEncodingBindingElement(MessageVersion.Soap12,Encoding.UTF8);
CustomBinding=新的CustomBinding(transportSecurity、textMessageEncoding、httpTransport);
EndpointAddress serviceAddress=新的EndpointAddress(addressDirect);
ChannelFactory ChannelFactory=新的ChannelFactory(绑定,服务地址);
UsernameClientCredentials=newusernameclientcredentials(newusernameinfo(username,password));
移除(typeof(ClientCredentials));
channelFactory.Endpoint.Behaviors.Add(凭据);
设备通道=channelFactory.CreateChannel();
var unitTime=channel.GetSystemDateAndTime(新的GetSystemDateAndTimeRequest());
tbInfo.AppendText(string.Format(“摄影机本地时间:{0}:{1}:{2}\n”,unitTime.SystemDateAndTime.LocalDateTime.Time.Hour,unitTime.SystemDateAndTime.LocalDateTime.Time.Minute,unitTime.SystemDateAndTime.LocalDateTime.Time.Second));
tbInfo.AppendText(string.Format(“相机UTC时间:{0}:{1}:{2}\n”,unitTime.SystemDateAndTime.UTCDateTime.Time.Hour,unitTime.SystemDateAndTime.UTCDateTime.Time.Minute,unitTime.SystemDateAndTime.UTCDateTime.Time.Second));
var info=channel.GetDeviceInformation(新的GetDeviceInformation请求());
Show(string.Format(“Model:{0}”,info.Model));

能否提供有关此代码段中使用的UsernameTokenParameters类的更多信息?我找不到这门课的来源。。。