示例#1
0
 /// <summary>
 /// Converts the specified <paramref name="location"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="location">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <param name="secureUriFormat">The naming format of the required query string parameters of the tamper protected URI. Default is <b>?token={0}&amp;iv={1}&amp;salt={2}</b>, where you can change the naming of the query string parameters.</param>
 /// <param name="querystringParameterHashName">The name of the checksum parameter to append to the tampering protected URI. Default is <b>hash</b>.</param>
 /// <returns>An URI equivalent to the <paramref name="location"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri CreateTamperingProtectedUri(Uri location, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType, string secureUriFormat, string querystringParameterHashName)
 {
     if (location == null)
     {
         throw new ArgumentNullException(nameof(location));
     }
     return(CreateTamperingProtectedUri(location.OriginalString, securityKey, settings, algorithmType, secureUriFormat, querystringParameterHashName));
 }
示例#2
0
        /// <summary>
        /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
        /// </summary>
        /// <param name="uriLocation">The URI to protect from tampering.</param>
        /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
        /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
        /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
        /// <param name="secureUriFormat">The naming format of the required query string parameters of the tamper protected URI. Default is <b>?token={0}&amp;iv={1}&amp;salt={2}</b>, where you can change the naming of the query string parameters.</param>
        /// <param name="querystringParameterHashName">The name of the checksum parameter to append to the tampering protected URI. Default is <b>hash</b>.</param>
        /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
        public static Uri CreateTamperingProtectedUri(string uriLocation, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType, string secureUriFormat, string querystringParameterHashName)
        {
            Validator.ThrowIfNullOrEmpty(uriLocation, nameof(uriLocation));
            Validator.ThrowIfNull(securityKey, nameof(securityKey));
            Validator.ThrowIfNull(settings, nameof(settings));
            Validator.ThrowIfEqual(securityKey.Length, 0, nameof(securityKey));
            Validator.ThrowIfNullOrEmpty(secureUriFormat, nameof(secureUriFormat));

            int foundArguments;

            if (!StringUtility.ParseFormat(secureUriFormat, 3, out foundArguments))
            {
                throw new ArgumentException("You must - in this order - specify three arguments for; 'token', 'iv' and 'salt'. This value cannot be exceeded nor the opposite. 'token', 'iv' and 'salt' is the default values.");
            }
            NameValueCollection formatedQuerytring = QueryStringConverter.FromString(secureUriFormat);

            SecurityToken securityToken = SecurityToken.Create(settings);

            byte[] iv = AdvancedEncryptionStandardUtility.GenerateInitializationVector();
            byte[] encryptedSecurityToken         = SecurityUtility.CreateEncryptedSecurityToken(securityToken, securityKey, iv);
            string ivAsString                     = HttpUtility.UrlEncode(Encoding.UTF8.GetString(iv, 0, iv.Length));
            string encryptedSecurityTokenAsString = HttpUtility.UrlEncode(Convert.ToBase64String(encryptedSecurityToken));
            string salt = HttpUtility.UrlEncode(StringUtility.CreateRandomString(18));
            int    indexOfQuestionMark    = uriLocation.IndexOf('?');
            string uriLocationQuerystring = indexOfQuestionMark > 0 ? uriLocation.Substring(indexOfQuestionMark) : "";

            uriLocation = indexOfQuestionMark > 0 ? uriLocation.Substring(0, indexOfQuestionMark) : uriLocation;

            NameValueCollection querystring       = QueryStringConverter.FromString(uriLocationQuerystring);
            NameValueCollection secureQuerystring = QueryStringConverter.FromString(string.Format(CultureInfo.InvariantCulture, secureUriFormat, encryptedSecurityTokenAsString, ivAsString, salt));

            secureQuerystring.Add(querystring);
            querystring = QueryStringUtility.RemoveDublets(secureQuerystring, formatedQuerytring.AllKeys);

            string secureUri = string.Format(CultureInfo.InvariantCulture, "{0}{1}", uriLocation, QueryStringConverter.FromNameValueCollection(querystring));

            secureUri += string.Format(CultureInfo.InvariantCulture, "&{0}={1}", querystringParameterHashName, HashUtility.ComputeHash(secureUri + salt + securityToken.Token, o =>
            {
                o.AlgorithmType = algorithmType;
                o.Encoding      = Encoding.UTF8;
            }).ToHexadecimal());
            return(new Uri(secureUri));
        }
示例#3
0
 /// <summary>
 /// Converts the specified <paramref name="location"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="location">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <returns>An URI equivalent to the <paramref name="location"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri CreateTamperingProtectedUri(Uri location, byte[] securityKey, SecurityTokenSettings settings)
 {
     if (location == null)
     {
         throw new ArgumentNullException(nameof(location));
     }
     return(CreateTamperingProtectedUri(location.OriginalString, securityKey, settings));
 }
示例#4
0
 /// <summary>
 /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="uriLocation">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri CreateTamperingProtectedUri(string uriLocation, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType)
 {
     return(CreateTamperingProtectedUri(uriLocation, securityKey, settings, algorithmType, "?token={0}&iv={1}&salt={2}"));
 }
示例#5
0
 /// <summary>
 /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="uriLocation">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <param name="secureUriFormat">The naming format of the required query string parameters of the tamper protected URI. Default is <b>?token={0}&amp;iv={1}&amp;salt={2}</b>, where you can change the naming of the query string parameters.</param>
 /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri CreateTamperingProtectedUri(string uriLocation, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType, string secureUriFormat)
 {
     return(CreateTamperingProtectedUri(uriLocation, securityKey, settings, algorithmType, secureUriFormat, "hash"));
 }
示例#6
0
 /// <summary>
 /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="uriLocation">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri CreateTamperingProtectedUri(string uriLocation, byte[] securityKey, SecurityTokenSettings settings)
 {
     return(CreateTamperingProtectedUri(uriLocation, securityKey, settings, HashAlgorithmType.SHA1));
 }
示例#7
0
 /// <summary>
 /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="uriLocation">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri ToProtectedUri(this string uriLocation, byte[] securityKey, SecurityTokenSettings settings)
 {
     return(WebSecurityUtility.CreateTamperingProtectedUri(uriLocation, securityKey, settings));
 }
示例#8
0
 /// <summary>
 /// Converts the specified <paramref name="location"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="location">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <param name="secureUriFormat">The naming format of the required query string parameters of the tamper protected URI. Default is <b>?token={0}&amp;iv={1}&amp;salt={2}</b>, where you can change the naming of the query string parameters.</param>
 /// <param name="querystringParameterHashName">The name of the checksum parameter to append to the tampering protected URI. Default is <b>hash</b>.</param>
 /// <returns>An URI equivalent to the <paramref name="location"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri ToProtectedUri(this Uri location, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType, string secureUriFormat, string querystringParameterHashName)
 {
     return(WebSecurityUtility.CreateTamperingProtectedUri(location, securityKey, settings, algorithmType, secureUriFormat, querystringParameterHashName));
 }
示例#9
0
 /// <summary>
 /// Converts the specified <paramref name="location"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="location">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <returns>An URI equivalent to the <paramref name="location"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri ToProtectedUri(this Uri location, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType)
 {
     return(WebSecurityUtility.CreateTamperingProtectedUri(location, securityKey, settings, algorithmType));
 }
示例#10
0
 /// <summary>
 /// Converts the specified <paramref name="uriLocation"/> to a tampering protected <see cref="Uri"/>.
 /// </summary>
 /// <param name="uriLocation">The URI to protect from tampering.</param>
 /// <param name="securityKey">The security key to use for the <see cref="SecurityToken"/> encryption.</param>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/>.</param>
 /// <param name="algorithmType">The hash algorithm to use for the URI checksum computation. Default is <b><see cref="HashAlgorithmType.SHA1"/></b>.</param>
 /// <param name="secureUriFormat">The naming format of the required query string parameters of the tamper protected URI. Default is <b>?token={0}&amp;iv={1}&amp;salt={2}</b>, where you can change the naming of the query string parameters.</param>
 /// <returns>An URI equivalent to the <paramref name="uriLocation"/> but protected from tampering - including but not limited to - MITM attacks.</returns>
 public static Uri ToProtectedUri(this string uriLocation, byte[] securityKey, SecurityTokenSettings settings, HashAlgorithmType algorithmType, string secureUriFormat)
 {
     return(WebSecurityUtility.CreateTamperingProtectedUri(uriLocation, securityKey, settings, algorithmType, secureUriFormat));
 }