语境:
我目前正在尝试编写一个 C# 应用程序,以使用 ONVIF“标准”从 IP 摄像头获取信息,例如摄像头馈送、日期时间、流 URL、配置文件等等……
我设法使用这段代码获得了相机的系统时间:
public bool Initialise(string cameraAddress, string userName, string password)
{
bool result = false;
try
{
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10)
};
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
{
AuthenticationScheme = AuthenticationSchemes.Digest
};
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
System.Net.ServicePointManager.Expect100Continue = false;
DeviceClient deviceClient = new DeviceClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
var temps = deviceClient.GetSystemDateAndTime();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
return result;
}
该deviceClient.GetSystemDateAndTime()
函数通过 http 发布此 XML/SOAP 信封:
当我通过 VS2017 调试器运行该函数时,我正在从 wireshark 读取这个 XML ......
猜想: 我正在尝试将 UsernameToken 类型的身份验证添加到我的代码中,这样我就可以执行需要这种类型身份验证的操作......
我试图将这些行添加到我的代码中:
deviceClient = new DeviceClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
deviceClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
,但它甚至没有向我发送的 XML 添加用户/密码位......
问题: 如何将此 usernameToken 添加到我发送的 XML/SOAP 信封的标题中?