Skip to main content
added 1353 characters in body
Source Link
Dmytro Shevchenko
  • 12.6k
  • 2
  • 45
  • 90

Solution

HashingUtils.ProtectAssetUrl(string url) and HashingUtils.GetAssetUrlHash(string url) take the full URL path into account when generating the hash. So there are no adjustments in the algorithm that you need to make.

Just use this code:

string imageUrl = // get the URL
imageUrl = HashingUtils.ProtectAssetUrl(imageUrl);

This is all you need to generate a URL with an added hash value.


Proof

Here's the (reformatted) source code of HashingUtils.GetAssetUrlHash():

public static string GetAssetUrlHash(string url)
{
    url = url.ToLowerInvariant();

    UrlString urlString = new UrlString(url);

    if (!HashingUtils.Proxy.IsMediaUrl(url))
    {
        return string.Empty;
    }

    Uri result;
    string urlPath = (Uri.TryCreate(url, UriKind.Absolute, out result) ? result.LocalPath : urlString.Path).TrimStart('/');

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.Append(string.Format("{0}?", urlPath));

    for (int i = 0; i < MediaManager.Config.RequestProtection.QueryParametersToProtect.Count; i++)
    {
        stringBuilder.Append(
            string.Format(
                "{0}={1}",
                MediaManager.Config.RequestProtection.QueryParametersToProtect[i],
                urlString.Parameters[MediaManager.Config.RequestProtection.QueryParametersToProtect[i]]));

        if (i < MediaManager.Config.RequestProtection.QueryParametersToProtect.Count - 1)
        {
            stringBuilder.Append("&");
        }
    }

    return HashingUtils.ComputeHash(stringBuilder.ToString());
}

As you can see, the URL path is used when creating the hash. It's appended to the stringBuilder:

stringBuilder.Append(string.Format("{0}?", urlPath));

The urlPath here will be equal to /images/{22EB368A-4AD3-48C8-B1EB-1BC14DE0F7BF}-200-100.jpg in your example.

HashingUtils.ProtectAssetUrl(string url) and HashingUtils.GetAssetUrlHash(string url) take the full URL into account when generating the hash. So there are no adjustments in the algorithm that you need to make.

Just use this code:

string imageUrl = // get the URL
imageUrl = HashingUtils.ProtectAssetUrl(imageUrl);

This is all you need to generate a URL with an added hash value.

Solution

HashingUtils.ProtectAssetUrl(string url) and HashingUtils.GetAssetUrlHash(string url) take the full URL path into account when generating the hash. So there are no adjustments in the algorithm that you need to make.

Just use this code:

string imageUrl = // get the URL
imageUrl = HashingUtils.ProtectAssetUrl(imageUrl);

This is all you need to generate a URL with an added hash value.


Proof

Here's the (reformatted) source code of HashingUtils.GetAssetUrlHash():

public static string GetAssetUrlHash(string url)
{
    url = url.ToLowerInvariant();

    UrlString urlString = new UrlString(url);

    if (!HashingUtils.Proxy.IsMediaUrl(url))
    {
        return string.Empty;
    }

    Uri result;
    string urlPath = (Uri.TryCreate(url, UriKind.Absolute, out result) ? result.LocalPath : urlString.Path).TrimStart('/');

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.Append(string.Format("{0}?", urlPath));

    for (int i = 0; i < MediaManager.Config.RequestProtection.QueryParametersToProtect.Count; i++)
    {
        stringBuilder.Append(
            string.Format(
                "{0}={1}",
                MediaManager.Config.RequestProtection.QueryParametersToProtect[i],
                urlString.Parameters[MediaManager.Config.RequestProtection.QueryParametersToProtect[i]]));

        if (i < MediaManager.Config.RequestProtection.QueryParametersToProtect.Count - 1)
        {
            stringBuilder.Append("&");
        }
    }

    return HashingUtils.ComputeHash(stringBuilder.ToString());
}

As you can see, the URL path is used when creating the hash. It's appended to the stringBuilder:

stringBuilder.Append(string.Format("{0}?", urlPath));

The urlPath here will be equal to /images/{22EB368A-4AD3-48C8-B1EB-1BC14DE0F7BF}-200-100.jpg in your example.

Source Link
Dmytro Shevchenko
  • 12.6k
  • 2
  • 45
  • 90

HashingUtils.ProtectAssetUrl(string url) and HashingUtils.GetAssetUrlHash(string url) take the full URL into account when generating the hash. So there are no adjustments in the algorithm that you need to make.

Just use this code:

string imageUrl = // get the URL
imageUrl = HashingUtils.ProtectAssetUrl(imageUrl);

This is all you need to generate a URL with an added hash value.