We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

Medium Priority
516 Views
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_ACCOUNTDISABLE = &H2
Const UF_HOMEDIR_REQUIRED = &H8
Const UF_LOCKOUT = &H10
Const UF_DONT_EXPIRE_PASSWD = &H10000
Const UF_PASSWORD_CANT_CHANGE = &H40

Dim objDomain, objUser, objGroup, intUserFlags, intNewUserFlags

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

' PARAMTERES MUST BE PASSED

username="testme"
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"
objUser.SetInfo
objUser.GetInfo

Thanks, I really appreciate your help.
Comment
Watch Question

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

http://www.15seconds.com/issue/020730.htm
http://www.asptoday.com/Content.aspx?id=1934
http://www.123aspx.com/redir.aspx?res=29213

Author

Commented:
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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

Commented:
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.
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.
AmerSharaf,
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

                if(DirectoryEntry.Exists(newADUser.Path))

                {

                    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!!)

                newADUser.Properties["sAMAccountName"].Add(sAMAccountName);

 

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

                newADUser.CommitChanges();

 

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

                newADUser.CommitChanges();

 

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

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

                Console.ReadLine();

            }

 

        }

    }

}

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.