【问题标题】:Embed .Net C# In Web Page?在网页中嵌入.Net C#?
【发布时间】:2011-03-18 01:21:23
【问题描述】:

我想在网页中嵌入一些执行一些简单加密/解密功能的 C# .Net 代码。这将是一个内部网页,因此用户将被隐式信任。有没有办法做到这一点?我将需要访问用户的 Windows-MY 密钥存储(通过 CAPI)以提取密钥进行解密并访问 LDAP 服务器以获取公钥进行加密。

【问题讨论】:

    标签: c# web-applications embed


    【解决方案1】:

    您可以使用 Silverlight。

    但是请注意,您也可以在 Javascript 中进行加密:

    【讨论】:

    • 我想使用用户的 Windows-MY 密钥存储中已经存在的密钥,并且我从其中读取的文件中,Siverlight 和 Javascript 都无法访问它们
    • @Petey:那你需要写一个浏览器插件。
    • 谢谢 SLaks(这应该是在 Ex Lax 上玩的吗?),我应该从哪里开始寻找有关浏览器插件的信息?
    • @Petey:哪些浏览器?谷歌是你的朋友。 (不,这是我的首字母)
    • 不是在和我妻子睡觉之后他不是。我在浏览器插件上找到了我需要的信息,再次感谢。
    【解决方案2】:

    定义“进入网页”是什么意思?网页由浏览器运行,这些浏览器通常只知道 Javascript(和 Java)。

    您可以将其作为 Silverlight 应用程序来实现。

    【讨论】:

    • Silverlight 没有我需要的加密功能。除非有办法访问我没有找到的 Windows-MY 密钥库?
    【解决方案3】:

    Silverlight 或者可能是 c# 到 JavaScript 的编译器,例如 Script#

    【讨论】:

      【解决方案4】:

      考虑编写一个新的 ASP.NET 应用程序,您的加密/解密逻辑位于应用程序中。或许可以创建一个新的网络表单应用程序,其中包含一个专门用于处理这些请求的页面。

      考虑在单独的 .NET 程序集中编写该加密逻辑,然后从您的 ASP.NET 应用程序中引用该程序集。

      尚不清楚您是否希望将此作为一项服务,或者用户是否希望在文本框中输入文本,并在访问时让它执行加密。

      【讨论】:

      • 这可行,但我需要一种方法来从他们的 Windows-MY 密钥库中获取用户的私钥以进行解密。其他一切都可以在服务器端完成。
      【解决方案5】:

      我最终使用 C# 伪造了一个 COM 对象,然后使用 JavaScript 调用该 COM 对象,并且能够通过这种方式通过浏览器与 CAPI 进行交互。

      JavaScript:

      <html>
      <head>
      <script language="javascript">
      var keystore = new ActiveXObject("RBCrypto.KeyStore");
      
      function getCertList()
      {
         try {
           keystore.openKeyStore("MY", true, false);
           var size = keystore.getStoreSize();
      
           var list = document.getElementById('list');
           list.size = size;
      
           for(var i = 0; i < size; i++)
           {
              var fname = keystore.getFriendlyName(i, true);
              var opt = new Option(fname, fname);
              list.options.add(opt);
           }
         }
         catch(err)
         {
           alert(err.description);
         }
      }
      </script>
      </head>
      <body onload="getCertList()">
         <center>
          <h2>KeyStore Test</h2>
          <hr />
          <br />
          <select id="list"></select>
         </center>
      </body>
      </html>
      

      C#:

      using System;
      using System.Runtime.InteropServices;
      using System.Security;
      using System.Security.Cryptography;
      using System.Security.Cryptography.X509Certificates;
      
      namespace RBCrypto
      {
          public interface AXInterface
          {
              void openKeyStore(string storeName, bool currentUser, bool readOnly);
              int getStoreSize();
              string getFriendlyName(int index, bool subjectNameIfEmpty);
          }
      
          [ClassInterface(ClassInterfaceType.AutoDual)]
          public class KeyStore :AXInterface
          {
              public void openKeyStore(string storeName, bool currentUser, bool readOnly)
              {
                  if (keystoreInitialized)
                      throw new Exception("Key Store must be closed before re-initialization");
      
                  try
                  {
                      if (currentUser) //user wants to open store used by the current user
                          certificateStore = new X509Store(storeName, StoreLocation.CurrentUser);
                      else             //user wants to open store used by local machine
                          certificateStore = new X509Store(storeName, StoreLocation.LocalMachine);
      
                      if (readOnly)
                          certificateStore.Open(OpenFlags.ReadOnly);
                      else
                          certificateStore.Open(OpenFlags.ReadWrite);
      
                      allCertificates = certificateStore.Certificates;
      
                      if (allCertificates == null)
                      {
                          certificateStore.Close();
                          throw new NullReferenceException("Certificates could not be gathered");
                      }
      
                      keystoreInitialized = true;
                  }
                  catch (ArgumentException ae)
                  {
                      throw ae;
                  }
                  catch (SecurityException se)
                  {
                      throw se;
                  }
                  catch (CryptographicException ce)
                  {
                      throw ce;
                  }
                  catch (NullReferenceException ne)
                  {
                      throw ne;
                  }
              }
      
              ....
         }
      }
      

      C# 组装信息:

      // Setting ComVisible to false makes the types in this assembly not visible 
      // to COM components.  If you need to access a type in this assembly from 
      // COM, set the ComVisible attribute to true on that type.
      [assembly: ComVisible(true)]
      

      为了使其工作,用户必须在他们的机器上安装您的 .dll(确保您在安装程序中指定将您的 .dll 注册为 vsdraCOM),并且他们必须将您的站点添加到他们信任的站点。

      【讨论】:

        【解决方案6】:

        您可以使用 AJAX 并通过网络调用您使用过的加密函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-12
          • 1970-01-01
          • 2011-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-08
          • 2011-10-19
          相关资源
          最近更新 更多