|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Impossible to use the attributes StringValidator/RegexValidator to ensure length of stringEg: 1: public class CryptoSection : ConfigurationSection 2: { 3: #region Construction 4: 5: public CryptoSection() 6: { 7: } 8: 9: #endregion 10: 11: #region Properties 12: 13: [ConfigurationProperty("decryptionKey", IsRequired = true, IsKey = false)] 14: [StringValidator(MinLength = 24, MaxLength = 24)] 15: public string DecryptionKey 16: { 17: get 18: { 19: return (string) this["decryptionKey"]; 20: } 21: set 22: { 23: this["decryptionKey"] = value; 24: } 25: } 26: 27: #endregion 28: } Then calling the following lines will throw an exception because for some reason when the validator gets invoked the first time this["decryptionKey"] will always be "" and therefore a length of 0: 1: CryptoSection cs = ConfigurationManager.GetSection("cryptoSection") as CryptoSection; 2: Response.Write("DecryptionKey = " + cs.DecryptionKey); The exception being thrown is: ArgumentException: The string must be at least 24 characters long.] System.Configuration.StringValidator.Validate(Object value) +247 System.Configuration.ConfigurationProperty.Validate(Object value) +27 [ConfigurationErrorsException: The value for the property 'decryptionKey' is not valid. The error is: The string must be at least 24 characters long.] System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line) +276 .... The only work-around i have found so far is by setting in the ConfigurationPropertyAttribute a default value with a length of 24 (Eg: 24 spaces), no need. There has to be a better way otherwise the the MinLength of the StringValidator is useless in this context because the value will always be a String.Empty when the validator is invoked. |
|||||||||||||||||||||||