繁体   English   中英

如何使用SOAP操作和HttpClient通过授权将ONVIF请求发送到网络摄像头

[英]How to send ONVIF request to web camera with authorization, using SOAP action and HttpClient

我正在研究在本地网络中发现和控制网络摄像头的项目。 我是一名C ++程序员,所以我不擅长.NET,但我们用C#编写这个项目,我遇到了一些问题。 我正在使用DiscoveryClient来发现本地网络中的所有设备。 接下来,我获取相机地址,创建HttpClient并尝试发送SOAP操作。 ONVIF规范: http ://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl。 某些操作(例如GetServiceCapabilities)会返回响应,但大多数操作会返回此错误:

<env:Body><env:Fault><env:Code><env:Value>env:Sender</env:Value>
<env:Subcode><env:Value>ter:NotAuthorized</env:Value>
</env:Subcode>
</env:Code>
<env:Reason><env:Text xml:lang="en">The action requested requires authorization and the sender is not authorized</env:Text>
</env:Reason>
</env:Fault>
</env:Body>

我正在创建类似官方ONVIF文档的SOAP请求(第35-36页)。 http://www.onvif.org/Portals/0/documents/WhitePapers/ONVIF_WG-APG-Application_Programmer's_Guide.pdf “admin”和“12345” - 来自我们的测试网络摄像头的登录名和密码。

这是我的代码,我尝试在下面发送请求:

HttpClient httpClient = new HttpClient();
var byteArray = Encoding.UTF8.GetBytes("admin:12345");

var request = requestStructure.CreateSoapRequest();

httpClient.DefaultRequestHeaders.Add("SOAPACTION", "\"" + requestStructure.actionNamespace + "#" + requestStructure.actionName + "\"");
httpClient.DefaultRequestHeaders.Add("Authorization", "Digest " + Convert.ToBase64String(byteArray));

var resp = await httpClient.PostAsync(requestedUri, new StringContent(request, UnicodeEncoding.UTF8));
var respString = await resp.Content.ReadAsStringAsync();

这是我的SOAP请求,由CreateSoapRequest()创建并返回:

public string CreateSoapRequest()
{
    var nonce64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.nonce.ToString()));
    var date64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.dateCreated));
    var password64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.password));
    SHA1 sha = new SHA1CryptoServiceProvider();
    var passwordDigest = sha.ComputeHash(Encoding.UTF8.GetBytes(nonce64 + date64 + password64));
    password64 = Convert.ToBase64String(passwordDigest);

    this.requestBodyString =
                    "<soap:Envelope "
                       + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                       + "soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                       + "<soap:Header>"
                            + "<Security s:mustUnderstand=\"1\" xmlns:w=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
                                + "<UsernameToken>"
                                    + "<Username>" + this.login + "</Username>"
                                    + "<Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">" + password64 + "</Password>"
                                    + "<Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">" + nonce64 + "</Nonce>"
                                    + "<Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" + this.dateCreated + "</Created>"
                                + "</UsernameToken>"
                            + "</Security>"
                       + "</soap:Header>"
                       + "<soap:Body>"
                            + "<u:" + this.actionName + " "
                            + "xmlns:u=\"" + this.actionNamespace + "\">"
                            + this.actionParameters
                            + "</u:" + this.actionName + ">"
                       + "</soap:Body>" +
                    "</soap:Envelope>\r\n\r\n";

    return this.requestBodyString;
}

谢谢你的帮助!

我最近一直在做很多Onvif的事情,并发现设置安全凭证非常繁琐。 首先,我要说确保您的日期格式如下:

 var now = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ss.fffZ");

与我的唯一区别(工作正常)是我在标题中有一个额外的行:

string xml = string.Format(@"<?xml version='1.0' encoding='UTF-8'?>"+
                                   "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'>" +
                                   "<s:Header>" +
                                   "<Security s:mustUnderstand='1' xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>" +
                                   "<UsernameToken>" +
                                   "<Username>{0}</Username>" +
                                   "<Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'>" +
                                   "{1}" +
                                   "</Password>" +
                                   "<Nonce EncodingType='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'>" +
                                   "{2}" +
                                   "</Nonce>" +
                                   "<Created xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>" +
                                   "{3}" +
                                   "</Created>" +
                                   "</UsernameToken>" +
                                   "</Security>" +
                                   "</s:Header>", user, credentials[0], b64Nonce, credentials[2]);

希望这可以帮助!

我已经处理了很长一段时间,这是我用来获取所有身份验证的功能:

    static void GetPasswordDigest()
    {
        //Get nonce
        Random rnd = new Random();
        Byte[] nonce_b = new Byte[16];
        rnd.NextBytes(nonce_b);
        nonce64 = Convert.ToBase64String(nonce_b);
        Console.WriteLine("Nonce: " + nonce64);

        //Get timestamp
        DateTime created = DateTime.Now;
        creationtime = created.ToString("yyyy-MM-ddTHH:mm:ssZ");
        Byte[] creationtime_b = Encoding.ASCII.GetBytes(creationtime);
        Console.WriteLine("Timestamp: " + creationtime);

        //Convert the plain password to bytes
        Byte[] password_b = Encoding.ASCII.GetBytes(ONVIFPassword);

        //Concatenate nonce_b + creationtime_b + password_b
        Byte[] concatenation_b = new byte[nonce_b.Length + creationtime_b.Length + password_b.Length];
        System.Buffer.BlockCopy(nonce_b, 0, concatenation_b, 0, nonce_b.Length);
        System.Buffer.BlockCopy(creationtime_b, 0, concatenation_b, nonce_b.Length, creationtime_b.Length);
        System.Buffer.BlockCopy(password_b, 0, concatenation_b, nonce_b.Length + creationtime_b.Length, password_b.Length);

        //Apply SHA1 on the concatenation
        SHA1 sha = new SHA1CryptoServiceProvider();
        Byte[] pdresult = sha.ComputeHash(concatenation_b);
        passworddigest = Convert.ToBase64String(pdresult);
        Console.WriteLine("Password digest: " + passworddigest);

    } 

我希望这是理所当然的。

暂无
暂无

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

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