MachineKeyValidationConverter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // System.Web.Configuration.MachineKeyValidationConverter
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. using System.Configuration;
  32. using System.Globalization;
  33. #if NET_2_0
  34. namespace System.Web.Configuration {
  35. public sealed class MachineKeyValidationConverter : ConfigurationConverterBase
  36. {
  37. #if NET_4_0
  38. const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES, HMACSHA256, HMACSHA384, HMACSHA512.";
  39. #else
  40. const string InvalidValue = "The enumeration value must be one of the following: SHA1, MD5, 3DES, AES.";
  41. #endif
  42. public MachineKeyValidationConverter ()
  43. {
  44. }
  45. public override object ConvertFrom (ITypeDescriptorContext ctx, CultureInfo ci, object data)
  46. {
  47. switch ((string) data) {
  48. case "MD5":
  49. return MachineKeyValidation.MD5;
  50. case "SHA1":
  51. return MachineKeyValidation.SHA1;
  52. case "3DES":
  53. return MachineKeyValidation.TripleDES;
  54. case "AES":
  55. return MachineKeyValidation.AES;
  56. #if NET_4_0
  57. case "HMACSHA256":
  58. return MachineKeyValidation.HMACSHA256;
  59. case "HMACSHA384":
  60. return MachineKeyValidation.HMACSHA384;
  61. case "HMACSHA512":
  62. return MachineKeyValidation.HMACSHA512;
  63. #endif
  64. default:
  65. throw new ArgumentException (InvalidValue);
  66. }
  67. }
  68. public override object ConvertTo (ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
  69. {
  70. #if NET_4_0
  71. if ((value == null) || (value.GetType () != typeof (MachineKeyValidation)))
  72. throw new ArgumentException (InvalidValue);
  73. #else
  74. if (value.GetType () != typeof (MachineKeyValidation)) {
  75. /* MS throws this exception on an invalid */
  76. throw new FormatException (InvalidValue);
  77. }
  78. #endif
  79. switch ((MachineKeyValidation) value) {
  80. case MachineKeyValidation.MD5:
  81. return "MD5";
  82. case MachineKeyValidation.SHA1:
  83. return "SHA1";
  84. case MachineKeyValidation.TripleDES:
  85. return "3DES";
  86. case MachineKeyValidation.AES:
  87. return "AES";
  88. #if NET_4_0
  89. case MachineKeyValidation.HMACSHA256:
  90. return "HMACSHA256";
  91. case MachineKeyValidation.HMACSHA384:
  92. return "HMACSHA384";
  93. case MachineKeyValidation.HMACSHA512:
  94. return "HMACSHA512";
  95. default:
  96. // includes MachineKeyValidation.Custom
  97. throw new ArgumentException (InvalidValue);
  98. #else
  99. default:
  100. /* MS throws this exception on an invalid */
  101. throw new FormatException (InvalidValue);
  102. #endif
  103. }
  104. }
  105. }
  106. }
  107. #endif