How can i make VBScript work in ASP.net ??

Posted on 2006-03-26
Medium Priority
Last Modified: 2013-11-25
Im new to VBscript and trying to create a User & email in my AD through a VBscript using ASP.net. In addition, I want to make the username and names Dynamic, meaning that the user will be able to type out the name he wants. Moreover, im trying to trigger the Vbscript when the user Clicks a Submit Button.

Note: Im using ASP.net Visual Studio 2005.

this is the VBscript Code:

Const UF_SCRIPT = &H1
Const UF_LOCKOUT = &H10

Dim objDomain, objUser, objGroup, intUserFlags, intNewUserFlags

Dim username, firstname, lastnames, Defaultcn, OUcontainer, OUType, group


firstname="Amer"           <==== all those parameters must be entered by the User from the ASP.net Form and then passed to the VBscript.
lastnames="Bin Test"
group = "grouptest"
fullname= firstname & " " & lastnames

Defaultcn    = "Users"
OUcontainer  = "UsersDirectory"
OUType = "StandardUsers"

Set objDomain = GetObject("LDAP://cn=" & Defaultcn & ",dc=MyDomain,dc=com")
Set objUser = objDomain.Create("user","cn=" & fullname)
objUser.Put "sAMAccountName", username
objUser.Put "userPrincipalName", username & "@MyDomain.com"

Thanks, I really appreciate your help.
Question by:amersharaf
  • 3
  • 2
  • 2
LVL 15

Expert Comment

ID: 16298366
You would want to put all of this in your codebehind. asp.net works very differently from old asp so I would not even try converting your old code. Rather rewrite it in the new way.

Take a look at these articles on how to admin AD from asp.net



Author Comment

ID: 16298514
Im not sure if i explained the situation clearly.

What im trying to do here is:

1- I have a working VBscript File.
1- I want to Paste the VBscript in ASP.net(or just execute the file from ASP.net) and create a Form.
2- Make the User enter some Values.
3- Toss the values back to the VBscript File.
4- when the User Clicks Submit, the VBscript file is executed.

I forgot to mention that the VBscript File works perfectly when executed outside of ASP.net.

I hope this clears any confusions.
LVL 15

Accepted Solution

GavinMannion earned 1000 total points
ID: 16298552
I would advise against it unless someone else knows how to protect that file. Using ASP.NET you have a lot more security on your application.

If you just pass the variables to a vbscript file to execute then someone can quite easily just call the vbscript file na dpass any variables they want creating as many users as they like without you being able to stop them.

What I would do is create a ASP.Net web form with all the textboxes you require and a button.

On button click rewrite the vbscript code into the codebehind and execute it there.

THis way you have complete control over who and when that code gets executed.

The links i posted explain how you create the AD interface and use it from there.
If someone else has some more experience in doing this please feel free to jump in. My knowledge on this is only theoretical.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


Expert Comment

ID: 16298658
I do Agree with GavinMannion, and in addition to what he said, you should consider that the VBScript is client side code which means it will be executed on the users machine - which may be not connected to your domain directly - so you should run your code through server-side code,other possible solution is to make your vbscript a COM component (with some minor efforts) and call this component from your ASP.NET code and pass to it the parameters you need.

Author Comment

ID: 16307612
GavinMannion, how can i do as what you suggested ? I tried rewriting the code, but it seems that the VBscript code doesnt get executed. and i dont know how to pass parameters to the vbscript from the code-behind.

Bedies, I forgot to mention that I'll be using the Form and VBscript on my Intranet, so security is not a big concern to me.
LVL 15

Expert Comment

ID: 16318589
If you have rewritten it into the codebehind then to pass the variables you would do something along the lines of

string username= txtUserName.Text
string firstname= txtFirstName.Text           <==== all those parameters must be entered by the User from the ASP.net Form and then

Even though the form is on the intranet only it doesn't mean someone in the company won't abuse it. Most hacking attacks and destructive actions come from inside your own company.

If you want you can paste your rewritten code into this thread and someone can point out where you are going wrong.

Assisted Solution

MohamedZaki earned 1000 total points
ID: 16319407
if you will rewrite the code, then check this code snippet, it's a sample to how to create a user on the domain in the .NET using DirectoryServices, wish it helps

P.S. the code is from this article ( http://blogs.msdn.com/federaldev/articles/472935.aspx )

namespace ADUserManager

    class Program


        static void Main(string[] args)


            string userName="John Smith", sAMAccountName="JSmith", passWord="SomeThingCkrit";

            // Step #1

            // Instantiate a Directory Entry Object to represent the User folder

            DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://CN=Users,DC=RSLAB,DC=Local");


            // Step #2

            // Check to make sure the folder is a container object

            if (adUserFolder.SchemaEntry.Name == "container")


                // Step #3

                // Create a Directory Entry Object to represent the user

                DirectoryEntry newADUser = adUserFolder.Children.Add("CN=John Smith", "User");


                // Step #4

                // Check to see if there is already someone in the folder with that name (CN)

                // If so, delete that account first



                    Console.WriteLine("The user: " + userName + " already exists.  The Account will be Overwritten!");


                    // Step 4a: Remove the user object first

                    adUserFolder.Children.Remove(new DirectoryEntry(newADUser.Path));



                //  Step 5: Add the user optional and required properties (sAMAccountName is ALWAYS REQUIRED!!)



                //  Step 6: Commit the changes to the AD data store



                //  Step 7: Set the password for the new account, which can only be done AFTER the account exists!

                //          We are using the "Invoke" method on the newADUser object, which uses Native AD Object under the hood to set

                //          the password.  

                newADUser.Invoke("setPassword", passWord);


                //  Step 8: Enable the user, if the user wants to, by setting the userAccountControl property

                //          to the magical value of 0x0200.  The disable-user value is 0x0002

                newADUser.Properties["userAccountControl"].Value = 0x0200;



                Console.WriteLine("User: " + userName + " successfully created!");

                Console.WriteLine("Hit any key to end...");








Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question