본문 바로가기
개발/C# .NET

자동 생성된 Machinekey 읽기

by xwing 2011. 4. 15.
Web.Config에 따로 설정을 하면 machinekey를 읽을 필요는 없겠지만서도.. 
나처럼 필요한 경우가 생길지도 몰라 구글링을 통해서 얻은 정보를 공유한다.


protected void Page_Load (object sender, EventArgs e)
{
  MachineKeySection section = (MachineKeySection) 
      ConfigurationManager.GetSection ("system.web/machineKey");

  BindingFlags flags = 
      BindingFlags.Instance | 
      BindingFlags.NonPublic | 
      BindingFlags.GetProperty;

  Func propertyReader = name => (byte[]) section
      .GetType ()
      .GetProperty (name, flags)
      .GetValue (section, null);

  string key = ConvertToHex (
    propertyReader ("DecryptionKeyInternal"));
    
  string iv = ConvertToHex (
    propertyReader ("ValidationKeyInternal"));
}

private string ConvertToHex (byte[] binary)
{
  return binary.Aggregate (
      new StringBuilder (), 
      (acc, c) => acc.AppendFormat ("{0:x2}", c), 
      acc => acc.ToString());
}// 뒤에요거 왜붙지? => 


출처 : 
http://aspnetresources.com/blog/how_to_read_auto_generated_machinekey 

'개발 > C# .NET' 카테고리의 다른 글

C# 한글깨짐 처리  (1) 2011.04.20
[C#] sysbase 접속  (0) 2011.02.21
Windows 2008 Server - WCF 404.7 error  (0) 2010.12.28