2014-01-08 5 views
1

해독 내가 필드를 차단PostSharp 필드를 차단 암호화하고 PostSharp를 사용

에 암호화/암호 해독을하고 싶은 나는 클래스

public class guestbookentry 
    {  
    [Encryption] // This Attribute has to Encrypt and Decrypt 
    public string Message { get; set; } 
    public string GuestName { get; set; }  
    } 

나는 푸른 테이블에서 객체를 저장하고 있습니다에게 있습니다. 특정 필드 만 En/Decrypt를 가져와야합니다. 이 필드를 차단

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using PostSharp; 
using PostSharp.Aspects; 
using EncryptionDecryption; 
using PostSharp.Serialization; 
using PostSharp.Aspects.Advices; 
using PostSharp.Extensibility; 

namespace GuestBook_Data 
{ 
[Serializable] 
public class EncryptionAttribute : LocationInterceptionAspect 
{  
    [MulticastPointcut(Targets = MulticastTargets.Field, Attributes = MulticastAttributes.Instance)] 
    public override void OnSetValue(LocationInterceptionArgs args) 
    { 
     base.OnSetValue(args); 
     if (args.Value != null) 
     {    
      MD5CryptoServiceExample objMD5Encrypt = new MD5CryptoServiceExample(); 
      args.Value = objMD5Encrypt.Encrypt(args.Value.ToString()).Replace(" ", "+"); 
      args.ProceedSetValue(); 
     } 
    } 

    public override void OnGetValue(LocationInterceptionArgs args) 
    { 
     base.OnGetValue(args); 
     if (args.Value != null) 
     {    
      MD5CryptoServiceExample objMD5Encrypt = new MD5CryptoServiceExample(); 
      args.Value = objMD5Encrypt.Decrypt(args.Value.ToString()); //objMD5Encrypt.Decrypt(args.Value.ToString()); 
      args.ProceedGetValue(); 
     } 
    } 
} 
} 

문제에

PostSharp 속성은 1. 연속 암호화하고 암호 해독 처리하기 곤란한 발생합니다.

친절 base.OnSetValue(args)를 호출하면 args.ProceedSetValue()를 호출하고, base.OnGetValue(args)를 호출 한 것과 같은 상태가된다 args.ProceedGetValue()를 호출하는 것과 동일하다는 것을,

답변

1

참고하시기 바랍니다. 즉, 각 처리기에서 메서드를 두 번 호출한다고 가정합니다.

는 당신이 필요가있는 무엇을 암호화 된 값을 읽을 OnGetValue의 시작 args.ProceedGetValue()를 호출하고 암호화 된 값을 저장하기 위해 OnSetValue의 끝에서 args.ProceedSetValue()를 호출하는 것입니다.

public override void OnGetValue(LocationInterceptionArgs args) 
{ 
    args.ProceedGetValue(); 
    if (args.Value != null) 
    { 
     args.Value = // decrypt 
    } 
} 

public override void OnSetValue(LocationInterceptionArgs args) 
{ 
    if (args.Value != null) 
    { 
     args.Value = // encrypt 
    } 
    args.ProceedSetValue(); 
} 

또한 [MulticastPointcut] 속성을 적용 할 필요가 없습니다. Developing Composite Aspects에 설명 된대로 복합 애스펙트를 개발할 때 사용됩니다.