开发者

Can I call a javascript function (from Classic ASP) from within my ASP.NET codebehind and obtain it's result?

开发者 https://www.devze.com 2023-02-08 02:57 出处:网络
From what I\'ve seen on SO and elsewhere, IIS can run server-side Javascript code using it\'s JScript engine.In fact, in Classic ASP, it appears fairly easy to get this to work, as VBScript can call t

From what I've seen on SO and elsewhere, IIS can run server-side Javascript code using it's JScript engine. In fact, in Classic ASP, it appears fairly easy to get this to work, as VBScript can call these functions directly. However, in .NET, I'm not sure how to procede.

I'm converting a web application written in Classic ASP into .NET. The application uses a javascript function to hash the users password. The result is queried against hashes stored in the database.

I'd like to continue to call this javascript function from my codebehind, so that the resulting hashes will continue to match.

Much of what i've seen online involves calling javascript using t开发者_开发百科he RegisterStartupScript function, which won't work in this case, since I want the javascript to run on the server and I want to acquire the result of the javascript before I post back. Is this possible?


Yes and no.

You can call a javascript method through the ASP.NET backend by but the only way your going to get the result is to post it back through AJAX

so you can start a call and then wait around until the result comes back and handle the result.

This may or may not be what you want.

To answer your question directly, rewrite the hashing function in C# codebehind and call it there. Don't just port legacy code, improve it.


Here's a way to wrap the server side JScript function using a simple local "web service" call.

Add a JScript or ASP script that references the hashing function:

File: DoHash.asp

<!-- #include file="passwordHasher.js" -->
<%
Dim password
password = Request.Form("password")
Response.Write HashPassword(password)

%>

In your ASP.NET application add this method (and class if necessary somewhere relevant:

public class HashWrapper
{
    public static string HashPasswordFromJScript(string password)
    {
      string url = "https://mysite.com/DoHash.asp";

      string fields = "password=" + password;
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
      request.ContentType = "application/x-www-form-urlencoded";
      request.Method = "POST";

      using(Stream rs = request.GetRequestStream())
      {
        using (MemoryStream ms = new MemoryStream())
        {
          using(BinaryWriter bw = new BinaryWriter(ms))
          {
            bw.Write(Encoding.UTF8.GetBytes(fields));
            ms.WriteTo(rs);
            ms.Flush();
          }
        }
      }

      using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
        if(response.StatusCode == HttpStatusCode.OK)
        {
          using(Stream rs = response.GetResponseStream())
          {
            using (StreamReader rr = new StreamReader(rs))
            {
              StringBuilder sb = new StringBuilder();
              int bufferSize = 1024;
              char[] read = new Char[bufferSize];
              int count = rr.Read(read, 0, bufferSize);

              while (count > 0)
              {
                sb.Append(read, 0, count);
                count = rr.Read(read, 0, bufferSize);
              }
              return sb.ToString();
            }
          }
        }
        return null;
      }
    }
}

In the part of the application you need to hash a password just call:

string hashedPassword = HashWrapper.HashPasswordFromJScript(password);

It's not pretty but it'd get you by. I'm also presuming your site can encrypt traffic using SSL.


The best thing to do would be to not be afraid of the hash function. If you can figure out the algorithm and reimplement it in C#. Come up with some test cases to check you get the same results through both. It'll save you a lot of effort in the end.

Because classic ASP parses pages on a page-by-page basis, you can pick and choose what language you wrote your pages in. You could use any activescript language (perl was available that way at one point!)

In a .Net web app, the whole app needs to be in one language. So to use Jscript.Net, you'd need a separate project for your function. The project could compile to an assembly that you import into your web app, or it could be another web app and you communicate between the 2 via requests or web services. Either way, it's a lot of work.

Simon


Try to use JavaScript handle client-side behaviors and let VB/C# handle the server side calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消