Created
May 18, 2013 19:45
-
-
Save vgrem/5605556 to your computer and use it in GitHub Desktop.
Storage for connection strings. Demonstrates the usage of SPPersistedObject and utilizes the method UpdateSecureStringValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecuredConnectionString : SPPersistedObject | |
{ | |
public SecuredConnectionString() | |
{ | |
} | |
public SecuredConnectionString(string name) | |
: base(name, SPFarm.Local) | |
{ | |
} | |
public void SetConnectionString(string key, string value) | |
{ | |
if (_connectionString == null) | |
_connectionString = new SPEncryptedString(key, this); | |
_connStringToUpdate = FromString(value); | |
} | |
public override void Update() | |
{ | |
if (_connStringToUpdate != null) | |
{ | |
_connectionString.UpdateSecureStringValue(this._connStringToUpdate); | |
_connStringToUpdate = null; | |
} | |
base.Update(); | |
} | |
private static SecureString FromString(string strIn) | |
{ | |
if (strIn == null) | |
return null; | |
var secureString = new SecureString(); | |
foreach (var c in strIn) | |
secureString.AppendChar(c); | |
secureString.MakeReadOnly(); | |
return secureString; | |
} | |
[Persisted] private SPEncryptedString _connectionString; | |
private SecureString _connStringToUpdate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment