{signature} :An HMAC-SHA256 signature string of the form: {URL-encoded-resourceURI} + "\n" + expiry. Important: The key is decoded from base64 and used as key to perform the HMAC-SHA256 computation.
再来看Azure Service Bus 的官方文档说明:
The signature for the SAS token is computed using the HMAC-SHA256 hash of a string-to-sign with the PrimaryKey property of an authorization rule.
两者的区别在于是否需要对秘钥进行decode。代码分别如下:
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key)); // decode the key
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); // don't decode the key
在《Azure IoT Hub 入门 - 权限管理》一文中,我跟大家share了生成SAS的source code 和DLL。该DLL 现支持生成Service Bus 和IoT Hub的SAS,两者通过TargetService来区分(源代码在这里)。调用方法如下: Service Bus: