Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Configuration;
- using System.Runtime.Serialization;
- namespace Hachette.Common.CustomConfigSections
- {
- /// <summary>
- /// Represents a userName and password in the app.config, which will be used by <see cref="AppSettings.cs"/>
- /// to encrypt/decrypt.
- /// </summary>
- public class EncryptedUserCredential : ConfigurationElement
- {
- [ConfigurationProperty("userName", IsRequired = true)]
- public string UserName
- {
- get
- {
- return this["userName"] as string;
- }
- }
- [ConfigurationProperty("password", IsRequired = true)]
- public string Password
- {
- get
- {
- return this["password"] as string;
- }
- }
- }
- public class EncryptedUserCredentials : ConfigurationElementCollection
- {
- public EncryptedUserCredential this[int index]
- {
- get
- {
- return base.BaseGet(index) as EncryptedUserCredential;
- }
- set
- {
- if (base.BaseGet(index) != null)
- {
- base.BaseRemoveAt(index);
- }
- this.BaseAdd(index, value);
- }
- }
- public new EncryptedUserCredential this[string responseString]
- {
- get
- {
- return (EncryptedUserCredential)BaseGet(responseString);
- }
- set
- {
- if (BaseGet(responseString) != null)
- {
- BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
- }
- BaseAdd(value);
- }
- }
- protected override ConfigurationElement CreateNewElement()
- {
- return new EncryptedUserCredential();
- }
- protected override object GetElementKey(ConfigurationElement element)
- {
- return ((EncryptedUserCredential)element).UserName;
- }
- }
- public class RegisterEncryptedUserCredentialsConfig : ConfigurationSection
- {
- public static RegisterEncryptedUserCredentialsConfig GetConfig()
- {
- //var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- return (RegisterEncryptedUserCredentialsConfig)System.Configuration.ConfigurationManager.GetSection("EncryptedUserCredentials") ?? new RegisterEncryptedUserCredentialsConfig();
- }
- [System.Configuration.ConfigurationProperty("EncryptedUserCredentials", IsDefaultCollection=true,IsRequired=true)]
- [ConfigurationCollection(typeof(EncryptedUserCredentials), AddItemName="EncryptedUserCredential")]
- public EncryptedUserCredentials EncryptedUserCredentials
- {
- get
- {
- object o = this["EncryptedUserCredentials"];
- return o as EncryptedUserCredentials;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement