Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     /// <summary>
  2.     /// <example>
  3.     /// public class SecuredController : Controller
  4.     //  {
  5.     //    [AuthorizeFromConfiguration(AppSettingRolesKey = "Roles")]
  6.     //    in config file add: <add key="Authorization.Roles" value="Role1,Role2"/>
  7.     //    public ActionResult Index()
  8.     //    {
  9.     //        return View();
  10.     //    }
  11.     //    [AuthorizeFromConfiguration(AppSettingUsersKey = "Users")]
  12.     //    in config file add: <add key="Authorization.Users" value="domain\user1,domain\user2"/>
  13.     //    public ActionResult Index()
  14.     //    {
  15.     //        return View();
  16.     //    }
  17.     //  }
  18.     //</example>
  19.     /// </summary>
  20. public class AuthorizeFromConfigurationAttribute : AuthorizeAttribute
  21.     {
  22.         public const string ConfigurationKeyPrefix = "Authorization.";
  23.         private string _appSettingRoleKey;
  24.         private string _appSettingUsersKey;
  25.  
  26.         public string AppSettingRolesKey
  27.         {
  28.             get { return _appSettingRoleKey; }
  29.             set
  30.             {
  31.                 if (!string.IsNullOrWhiteSpace(value))
  32.                 {
  33.                     _appSettingRoleKey = value;
  34.                     string roles = GetAppSettingValue(_appSettingRoleKey);
  35.                     if (!string.IsNullOrWhiteSpace(roles))
  36.                         Roles = roles;
  37.                 }
  38.             }
  39.         }
  40.  
  41.         private static string GetAppSettingValue(string key)
  42.         {
  43.             if (string.IsNullOrWhiteSpace(key))
  44.             {
  45.                 throw new ArgumentNullException("key");
  46.             }
  47.             if (key.IndexOf(ConfigurationKeyPrefix,StringComparison.OrdinalIgnoreCase) < 0)
  48.             {
  49.                 key = string.Format("{0}{1}", ConfigurationKeyPrefix, key);
  50.             }
  51.  
  52.             string keyValue = string.Empty;
  53.             if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
  54.             {
  55.                 keyValue = ConfigurationManager.AppSettings[key];
  56.             }
  57.             else
  58.             {
  59.                 throw new Exception(string.Format("The key \"{0}\" has not been defined in the configuration file", key));
  60.             }
  61.             return keyValue;
  62.         }
  63.  
  64.         public string AppSettingUsersKey
  65.         {
  66.             get { return _appSettingUsersKey; }
  67.             set
  68.             {
  69.                 if (!string.IsNullOrWhiteSpace(value))
  70.                 {
  71.                     _appSettingUsersKey = value;
  72.                     string users = GetAppSettingValue(_appSettingUsersKey);
  73.                     if (!string.IsNullOrWhiteSpace(users))
  74.                         Users = users;
  75.                 }
  76.             }
  77.         }
  78.     }