Created
August 30, 2012 03:12
-
-
Save jkuemerle/3521954 to your computer and use it in GitHub Desktop.
Attribute to decorate the type declaration for a class that has auto encrypted properties
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Security.Cryptography; | |
| using PostSharp.Aspects; | |
| using PostSharp.Aspects.Advices; | |
| using PostSharp.Aspects.Dependencies; | |
| namespace DataAccess.Aspects { | |
| [Serializable] | |
| [AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(EncryptedAttribute))] | |
| [IntroduceInterface(typeof(IEncrypted), OverrideAction = InterfaceOverrideAction.Ignore)] | |
| [AttributeUsage(AttributeTargets.Class)] | |
| public class EncryptedTypeAttribute : InstanceLevelAspect, IEncrypted { | |
| [IntroduceMember(IsVirtual=false,OverrideAction=MemberOverrideAction.OverrideOrFail, Visibility=PostSharp.Reflection.Visibility.Public)] | |
| public Dictionary<string, string> EncryptedValues { get; set; } | |
| public override void RuntimeInitialize(Type type) { | |
| EncryptedValues = new Dictionary<string, string>(); | |
| } | |
| public object AsClear(string Name) { | |
| if (null != EncryptedValues) { | |
| if (EncryptedValues.ContainsKey(Name)) | |
| return System.Text.UnicodeEncoding.Unicode.GetString(ProtectedData.Unprotect( | |
| Convert.FromBase64String(EncryptedValues[Name].Substring(136)), | |
| Convert.FromBase64String(EncryptedValues[Name].Substring(0, 136)), | |
| DataProtectionScope.CurrentUser)); | |
| } | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment