Advertisement
garfbradaz

Class for Stack Question

Oct 19th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Runtime.Serialization;
  7.  
  8. namespace Hachette.Common.CustomConfigSections
  9. {
  10.     /// <summary>
  11.     /// Represents a userName and password in the app.config, which will be used by <see cref="AppSettings.cs"/>
  12.     /// to encrypt/decrypt.
  13.     /// </summary>
  14.     public class EncryptedUserCredential : ConfigurationElement
  15.     {
  16.         [ConfigurationProperty("userName", IsRequired = true)]
  17.         public string UserName
  18.         {
  19.             get
  20.             {
  21.                 return this["userName"] as string;
  22.             }
  23.         }
  24.  
  25.         [ConfigurationProperty("password", IsRequired = true)]
  26.         public string Password
  27.         {
  28.             get
  29.             {
  30.                 return this["password"] as string;
  31.             }
  32.         }
  33.     }
  34.  
  35.     public class EncryptedUserCredentials : ConfigurationElementCollection
  36.     {
  37.         public EncryptedUserCredential this[int index]
  38.         {
  39.             get
  40.             {
  41.                 return base.BaseGet(index) as EncryptedUserCredential;
  42.             }
  43.             set
  44.             {
  45.                 if (base.BaseGet(index) != null)
  46.                 {
  47.                     base.BaseRemoveAt(index);
  48.                 }
  49.                 this.BaseAdd(index, value);
  50.             }
  51.         }
  52.  
  53.         public new EncryptedUserCredential this[string responseString]
  54.         {
  55.             get
  56.             {
  57.                 return (EncryptedUserCredential)BaseGet(responseString);
  58.             }
  59.             set
  60.             {
  61.                 if (BaseGet(responseString) != null)
  62.                 {
  63.                     BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
  64.                 }
  65.                 BaseAdd(value);
  66.             }
  67.         }
  68.  
  69.         protected override ConfigurationElement CreateNewElement()
  70.         {
  71.             return new EncryptedUserCredential();
  72.         }
  73.  
  74.         protected override object GetElementKey(ConfigurationElement element)
  75.         {
  76.             return ((EncryptedUserCredential)element).UserName;
  77.         }
  78.     }
  79.  
  80.  
  81.        
  82.     public class RegisterEncryptedUserCredentialsConfig : ConfigurationSection
  83.     {
  84.         public static RegisterEncryptedUserCredentialsConfig GetConfig()
  85.         {
  86.             //var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  87.  
  88.             return (RegisterEncryptedUserCredentialsConfig)System.Configuration.ConfigurationManager.GetSection("EncryptedUserCredentials") ?? new RegisterEncryptedUserCredentialsConfig();
  89.         }
  90.  
  91.         [System.Configuration.ConfigurationProperty("EncryptedUserCredentials", IsDefaultCollection=true,IsRequired=true)]
  92.         [ConfigurationCollection(typeof(EncryptedUserCredentials), AddItemName="EncryptedUserCredential")]
  93.         public EncryptedUserCredentials EncryptedUserCredentials
  94.         {
  95.             get
  96.             {
  97.                 object o = this["EncryptedUserCredentials"];
  98.                 return o as EncryptedUserCredentials;
  99.             }
  100.  
  101.  
  102.         }
  103.      
  104.     }
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement