OssRequestSigner.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using Aliyun.OSS.Common.Authentication;
  9. using Aliyun.OSS.Common.Communication;
  10. using Aliyun.OSS.Util;
  11. namespace Aliyun.OSS.Util
  12. {
  13. internal class OssRequestSigner : IRequestSigner
  14. {
  15. private readonly string _resourcePath;
  16. public OssRequestSigner(String resourcePath)
  17. {
  18. _resourcePath = resourcePath;
  19. }
  20. public void Sign(ServiceRequest request, ICredentials credentials)
  21. {
  22. var accessKeyId = credentials.AccessKeyId;
  23. var accessKeySecret = credentials.AccessKeySecret;
  24. var httpMethod = request.Method.ToString().ToUpperInvariant();
  25. // Because the resource path to is different from the one in the request uri,
  26. // can't use ServiceRequest.ResourcePath here.
  27. var resourcePath = _resourcePath;
  28. if (!string.IsNullOrEmpty(accessKeySecret))
  29. {
  30. var canonicalString = SignUtils.BuildCanonicalString(httpMethod, resourcePath, request);
  31. var signature = ServiceSignature.Create().ComputeSignature(accessKeySecret, canonicalString);
  32. request.Headers.Add(HttpHeaders.Authorization, "OSS " + accessKeyId + ":" + signature);
  33. }
  34. }
  35. }
  36. }