2011-06-25 12 views
0

イム、ウィッヒは、いくつかのSQLデータベースにCONECTといくつかの詳細は、データベースを形成し得るでしょう、ロードAランタイムでアセンブリおよびアプリケーションを作成するメソッドを呼び出すと、アセンブリをアンロード

ユーザー名のパスワードなどの詳細。はい、そのprittyストレートフォワードとシンプルだけで資格情報を復号するためのmetodを書いてください。

私の場合は、資格情報を解読するために第三者の暗号化機構に頼らざるを得ません。より多くの私はいくつかの他の暗号化方法を再び使用するいくつかのSQLサーバーに接続する必要があります。したがって、暗号化アセンブリを動的にロードし、暗号化メソッドを呼び出すアプリケーションを記述しています。

しかし、アセンブリフォームAssembly.LoadFile( "Path")をロードするとき、ロードされたアセンブリをアンロードできません。私は別のアプリドメインにこのアセンブリをロードし、relavantメソッドを呼び出してそのappdomainをアンロードすると思います。私はこの部分についていくつかの助けが必要です。私の欠点のために私は必要な方法を呼び出すことはできません。私のコードは次のとおりです。これについて私を助けてください。

クラスApplicationSettings {

private static ApplicationSettings m_ApplicationSettings; 
    public String m_ServerName { get; private set; } 
    public String m_DatabaseName { get; private set; } 
    public String m_UserID { get; private set; } 
    public String m_Password { get; private set; } 
    public String m_EncryptionDLLPath{ get; private set; } 
    public String m_NameSpace { get; private set; } 
    public String m_ClassName { get; private set; } 
    public String m_EncryptionMethodName { get; private set; } 
    public String m_DecryptionMethodName { get; private set; } 

    private ApplicationSettings() 
    { 
     m_ApplicationSettings = this; 
    } 

    public static ApplicationSettings CurrentValues 
    { 
     get 
     {     
      return m_ApplicationSettings; 
     } 
     private set 
     { 
      m_ApplicationSettings = value; 
     } 
    } 

    internal static void Initialize() 
    { 
     CommonFunctions.DataEncryption _enc = new CommonFunctions.DataEncryption(); 


     ApplicationSettings.CurrentValues = new ApplicationSettings(); 
     ApplicationSettings.CurrentValues.m_EncryptionDLLPath = @"C:\Users\Administrator\Documents\Visual Studio 2010\Projects\TestApp\TestApp\bin\Debug\AppSec.dll"; 
     ApplicationSettings.CurrentValues.m_NameSpace = "AppSec"; 
     ApplicationSettings.CurrentValues.m_ClassName = "AppSecEncDec"; 
     ApplicationSettings.CurrentValues.m_EncryptionMethodName = "Encrypt"; 
     ApplicationSettings.CurrentValues.m_DecryptionMethodName = "Decrypt"; 
     ApplicationSettings.CurrentValues.m_Password = _enc.Decrypt("pzBS3EJDoGM="); 
     ApplicationSettings.CurrentValues.m_UserID = "sa"; 

    } 



} 

クラスDataEncryption {予め

AppDomain DomainName;   

    //Call the Encryption Method 
    public String Encrypt(Object _DataToEncrypt) 
    { 


    } 

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt) 
    { 
     String _Decrypt = ""; 

     String assemblyFileName = ApplicationSettings.CurrentValues.m_EncryptionDLLPath; 
     String assemblyName = ApplicationSettings.CurrentValues.m_NameSpace; 

     //Setup the evidence 
     Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence); 
     AppDomain TestDomain = AppDomain.CreateDomain(
      "TestDomain", //The friendly name of the domain. 
      evidence, //Evidence mapped through the security policy to establish a top-of-stack permission set. 
      AppDomain.CurrentDomain.BaseDirectory, // The base directory that the assembly resolver uses to probe for assemblies. 
      System.IO.Path.GetFullPath(assemblyFileName), // The path relative to the base directory where the assembly resolver should probe for private assemblies. 
      true // If true, a shadow copy of an assembly is loaded into this application domain. 
     ); 
     string s = TestDomain.Load(assemblyName).FullName; 
     string[] myparam = new String[1]; 
     myparam[0] = "test"; 


     TestDomain.CreateInstance(TestDomain.Load(assemblyName).GetName().ToString(), ApplicationSettings.CurrentValues.m_NameSpace + "." + ApplicationSettings.CurrentValues.m_ClassName).CreateObjRef(GetType()); 
     //her i need to execute the Encrypt method which will load form the third party encryption mechanisam 

     //method name will be returnd on this parameter in application settings Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName ; 

     UloadAssembly(); 

     return _Decrypt; 
    } 


    public void UloadAssembly() 
    { 
     //Unload the loaded appdomain 
     AppDomain.Unload(DomainName);    
     GC.Collect(); 
    } 


} 

おかげ。

答えて

0

私はオーバー状況に

 public String Encrypt(Object _DataToEncrypt) 
    { 
     try 
     { 

      String _Encrypt = ""; 
      LoadAssembly(); 
      ShowLoadedAssemblies(); 
      if (ClassInstance != null) 
      { 
       MethodInfo EncryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_EncryptionMethodName); ; 
       if (EncryptionMethod != null) 
       { 
        object[] myparam = new object[1]; 
        myparam[0] = _DataToEncrypt; 
        _Encrypt = (string)EncryptionMethod.Invoke(null, myparam); 
       } 

      } 

      UloadAssembly(); 
      ShowLoadedAssemblies(); 
      return _Encrypt; 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.Message); 


     } 

    } 

    //Call the Decryption Method 
    public String Decrypt(Object _DataToDecrypt) 
    { 
     String _Decrypt = ""; 

     LoadAssembly(); 
     ShowLoadedAssemblies(); 
     if (ClassInstance != null) 
     { 
      MethodInfo DecryptionMethod = ClassInstance.GetType().GetMethod(Classes.ApplicationSettings.CurrentValues.m_DecryptionMethodName);; 
      if (DecryptionMethod != null) 
      { 
       object[] myparam = new object[1]; 
       myparam[0] = _DataToDecrypt; 
       _Decrypt = (string)DecryptionMethod.Invoke(null, myparam); 
      }    

     } 
     UloadAssembly(); 
     ShowLoadedAssemblies(); 
     return _Decrypt; 
    } 
    //Loading the Assembly 
    public void LoadAssembly() 
    { 




     Evidence evi = new Evidence(AppDomain.CurrentDomain.Evidence); 

     DomainName = AppDomain.CreateDomain(Classes.ApplicationSettings.CurrentValues.m_NameSpace 
              , evi 
              , AppDomain.CurrentDomain.BaseDirectory 
              , Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath 
              , true 
              ); 

     String LoadingAssemblyName = AssemblyName.GetAssemblyName(Classes.ApplicationSettings.CurrentValues.m_EncryptionDLLPath).FullName; 

     ClassInstance = DomainName.CreateInstanceAndUnwrap(LoadingAssemblyName 
                  , Classes.ApplicationSettings.CurrentValues.m_NameSpace 
                   + "." 
                   + Classes.ApplicationSettings.CurrentValues.m_ClassName 
                  ); 

    } 
    public void UloadAssembly() 
    { 
     //Unload the loaded appdomain 
     AppDomain.Unload(DomainName);    
     GC.Collect(); 
    } 
を来るために使用される場合は、下記のコードを見つけてくださいこれを行う方法を考え出し、完全にそれが成功することを願っています
関連する問題