Embed .Net C # in a web page?

I would like to add C # .Net code that performs some simple encryption / decryption functions on a web page. This will be an internal web page, so users will be implicitly trusted. Is there any way to do this? I will need to hit the Windows-MY custom key store (via CAPI) to pull the keys for decryption and click on the LDAP server to grab the public keys for encryption.

+3
source share
6 answers

I ended up tricking a COM object with C #, and then using JavaScript to call this COM object, and was able to interact with CAPI through a browser in this way.

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>

WITH#:

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;
            }
        }

        ....
   }
}

# AssemblyInfo:

// 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 ), .

+1
+4

, " -"? - , Javascript ( Java).

Silverlight.

+4

Silverlight , , a# JavaScript, Script #.

+1

ASP.NET, / . , webforms , .

.NET, ASP.NET.

, , .

+1

AJAX , .

0

Source: https://habr.com/ru/post/1755356/


All Articles