コード例 #1
0
    /// <summary>
    ///   Gets the value for the specified element, assuming it's enabled, and based on the type of specified element's source,
    ///   looks up its value.
    /// </summary>
    /// <param name="element">The configuration element.</param>
    /// <returns>The target value, or null if one is not found.</returns>
    public static string GetValue(SourceElement element) {

      /*------------------------------------------------------------------------------------------------------------------------
      | Validate input
      \-----------------------------------------------------------------------------------------------------------------------*/
      Contract.Requires<ArgumentNullException>(HttpContext.Current != null, "Assumes the current HTTP context is available.");

      /*------------------------------------------------------------------------------------------------------------------------
      | Return null if the element is disabled or missing
      \-----------------------------------------------------------------------------------------------------------------------*/
      if (element == null || !element.Enabled) return null;

      /*------------------------------------------------------------------------------------------------------------------------
      | Pull value from support source
      \-----------------------------------------------------------------------------------------------------------------------*/
      string value = null;

      switch (element.Source?.ToUpperInvariant()?? "") {
        case("QUERYSTRING") :
          value         = HttpContext.Current.Request.QueryString[element.Location];
          break;
        case("FORM") :
          value         = HttpContext.Current.Request.Form[element.Location];
          break;
        case("APPLICATION") :
          value         = (string)HttpContext.Current.Application?[element.Location];
          break;
        case("SESSION") :
          value         = (string)HttpContext.Current.Session?[element.Location];
          break;
        case("COOKIE") :
          value         = HttpContext.Current.Request.Cookies[element.Location]?.Value;
          break;
        case("ROLE") :
          value         = Roles.IsUserInRole(element.Location).ToString(CultureInfo.InvariantCulture);
          break;
        case("HOSTNAME") :
          value         = element.Location;
          break;
        case("URL") :
          if (Int32.Parse(element.Location, CultureInfo.InvariantCulture) >= 0) {
            value       = HttpContext.Current.Request.Path.Split('/')[Int32.Parse(element.Location, CultureInfo.InvariantCulture)];
          }
          break;
        default :
          throw new ConfigurationErrorsException("The source '" + element.Source + "' in the web.config is invalid.");
      }

      return value;

    }
コード例 #2
0
    /// <summary>
    ///   Looks up a source element at a given location, identifies the source value, and verifies whether the element is
    ///   available, enabled, or set to true.
    /// </summary>
    /// <param name="element">The target <see cref="SourceElement"/>.</param>
    /// <param name="evaluateValue">Boolean indicator noting whether to use the Value to determine enabled status.</param>
    /// <returns>
    ///   Boolean value representing whether or not the source is available, enabled or set to true.
    /// </returns>
    public static bool IsEnabled(SourceElement element, bool evaluateValue) {

      if (element == null) {
        return false;
      }
      if (!element.Enabled) {
        return false;
      }
      else if (!evaluateValue || element.Location == null) {
        return true;
      }

      var value = GetValue(element);

      if (String.IsNullOrEmpty(value)) return false;

      return Convert.ToBoolean(value, CultureInfo.InvariantCulture);

    }
コード例 #3
0
 /// <summary>
 ///   Determines whether the specified element is trusted via its availability.
 /// </summary>
 /// <param name="element">The <see cref="SourceElement"/> object.</param>
 /// <returns>
 ///   Boolean value representing whether the specified element is available.
 /// </returns>
 public static bool IsTrusted(SourceElement element) => (element == null)? false : element.Trusted;