Creating Virtual Directory

J

Jaydeep

Hi,
Anybody knows how to create virtual directory programmatically under root
directory ofcourse from code-behind.
I am developing web-based application where I need to create a folder and
making this folder as virtual directory in IIS.

I know in ASP how to do it.
Set objIIS = GetObject("IIS://localhost/W3SVC/1/Root")
objIIS.Create("IISWebVirtualDir", strVirtualDirectoryName)

but how to do this in ASP.NET using C# ?
What is the namespace I should include so that I will get object of Root
directory of IIS and then I can create virtual directory ?

TIA
Jaydeep
 
J

Jaydeep

Hi Lionel,
I just saw the link. By reading the given web page what I realised that
1) it will not work on Win2K prof / Server machine.
"This example requires Windows XP Professional Service Pack 2 or Windows
Server 2003 Service Pack 1."
2) Also I tried import System.DirectoryServices namespace in the code-behind
of my ASPX page, but enable to do so,there is no such namespace found, dont
know the exact reason. But I guess it is related to ADSI, Active Directory
Service
3) The code given in the link is also incomplete
These are my initial observations. Still searching good link/example
TIA
Jaydeep
 
L

Lionel LASKE

Jaydeep said:
Hi Lionel,
I just saw the link. By reading the given web page what I realised that
1) it will not work on Win2K prof / Server machine.
"This example requires Windows XP Professional Service Pack 2 or Windows
Server 2003 Service Pack 1."
This requirement is just for "enumerate properties". If you already know
wich properties you need, you don't have to enumerate properties.
2) Also I tried import System.DirectoryServices namespace in the
code-behind
of my ASPX page, but enable to do so,there is no such namespace found,
dont
know the exact reason. But I guess it is related to ADSI, Active Directory
Service
Just in case: Are you sure you add to your project a reference to
"System.DirectoryServices.dll" ?
 
J

Jaydeep

Hi Lionel,
Thanks for your prompt replies. sorry I forgot to add reference to
System.DirectoryService, my mistake.
I am getting "Access denied" error, when I tried to execute this code.

private void button1_Click(object sender, System.EventArgs e)
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
DirectoryEntry deRoot= new DirectoryEntry("IIS://localhost" +
strRootSubPath);
try
{
DirectoryEntry deNewVDir =
deRoot.Children.Add("TestVirDir",strSchema); // I am getting error on this
line.
deNewVDir.Properties["Path"].Insert(0,"c:\\TestVirDir"); // Just for
example.
deNewVDir.CommitChanges();
deRoot.CommitChanges();

// Create a Application
if (strSchema == "IIsWebVirtualDir")
deNewVDir.Invoke("AppCreate",true);

// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
Response.Write("Virtual Directory TestVirDir has been created");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}

Interestingly Same code logic works when I create windows application. It
seems it is not allowing me to create Virtual Dir through web-based
application(ASP.NET & C#) because IIS is in use and this is why access is
denied error comes. So what could be the solution for this ?

TIA
Jaydeep
 
L

Lionel LASKE

I don't think it's related to current use of IIS metabase.
My guess is that your "access denied" is related to the security permission
of the virtual directory for your ASP.NET app.
Are you using "anonymous" access ? if so I think your IUSR_XXXX don't have
permission to make admin task on IIS. Unfortunatly, I don't know what
permission is required to handle IIS metabase :)
You'd better to uncheck anonymous access, use integrated security and run
your app with an admin account.

Lionel.


Jaydeep said:
Hi Lionel,
Thanks for your prompt replies. sorry I forgot to add reference to
System.DirectoryService, my mistake.
I am getting "Access denied" error, when I tried to execute this code.

private void button1_Click(object sender, System.EventArgs e)
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
DirectoryEntry deRoot= new DirectoryEntry("IIS://localhost" +
strRootSubPath);
try
{
DirectoryEntry deNewVDir =
deRoot.Children.Add("TestVirDir",strSchema); // I am getting error on this
line.
deNewVDir.Properties["Path"].Insert(0,"c:\\TestVirDir"); // Just
for
example.
deNewVDir.CommitChanges();
deRoot.CommitChanges();

// Create a Application
if (strSchema == "IIsWebVirtualDir")
deNewVDir.Invoke("AppCreate",true);

// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
Response.Write("Virtual Directory TestVirDir has been created");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}

Interestingly Same code logic works when I create windows application. It
seems it is not allowing me to create Virtual Dir through web-based
application(ASP.NET & C#) because IIS is in use and this is why access is
denied error comes. So what could be the solution for this ?

TIA
Jaydeep


Lionel LASKE said:
This requirement is just for "enumerate properties". If you already know
wich properties you need, you don't have to enumerate properties.

Just in case: Are you sure you add to your project a reference to
"System.DirectoryServices.dll" ?
 
S

Scott Allen

Assuming you are using a default configuration and not impersonating,
your ASP.NET code is trying to modify the IIS metabase as the ASPNET
user (Win2k, XP) or the NETWORK SERVICE user (Win2k3). These are the
accounts used to execute ASP.NET code by default, and the accounts
have limited permissions. When run as a win forms app it works because
Windows sees your identity as the one trying to modify the metabase.

Only users in the Operators and Administrators group can modify the
IIS metabase. I don't recommend putting the asp.net account into
either of these groups. Instead, for the page that needs to execute
this code, use impersonation, i.e.:

<authentication mode="Windows" />

<authorization>
<deny users="?" />
<allow users="*" />
</authorization>

<identity impersonate="true" />


Hope that gives you some information to go on,

--
Scott
http://www.OdeToCode.com/blogs/scott/


Hi Lionel,
Thanks for your prompt replies. sorry I forgot to add reference to
System.DirectoryService, my mistake.
I am getting "Access denied" error, when I tried to execute this code.

private void button1_Click(object sender, System.EventArgs e)
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
DirectoryEntry deRoot= new DirectoryEntry("IIS://localhost" +
strRootSubPath);
try
{
DirectoryEntry deNewVDir =
deRoot.Children.Add("TestVirDir",strSchema); // I am getting error on this
line.
deNewVDir.Properties["Path"].Insert(0,"c:\\TestVirDir"); // Just for
example.
deNewVDir.CommitChanges();
deRoot.CommitChanges();

// Create a Application
if (strSchema == "IIsWebVirtualDir")
deNewVDir.Invoke("AppCreate",true);

// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
Response.Write("Virtual Directory TestVirDir has been created");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}

Interestingly Same code logic works when I create windows application. It
seems it is not allowing me to create Virtual Dir through web-based
application(ASP.NET & C#) because IIS is in use and this is why access is
denied error comes. So what could be the solution for this ?

TIA
Jaydeep


Lionel LASKE said:
This requirement is just for "enumerate properties". If you already know
wich properties you need, you don't have to enumerate properties.

Just in case: Are you sure you add to your project a reference to
"System.DirectoryServices.dll" ?
 
J

Jaydeep

Hi...
Got the solution... it was not that tough... added impersonation, userName
and password in my web.config file...

Thanks
Jaydeep

Jaydeep said:
Hi Lionel,
Thanks for your prompt replies. sorry I forgot to add reference to
System.DirectoryService, my mistake.
I am getting "Access denied" error, when I tried to execute this code.

private void button1_Click(object sender, System.EventArgs e)
{
strSchema = "IIsWebVirtualDir";
strRootSubPath = "/W3SVC/1/Root";
DirectoryEntry deRoot= new DirectoryEntry("IIS://localhost" +
strRootSubPath);
try
{
DirectoryEntry deNewVDir =
deRoot.Children.Add("TestVirDir",strSchema); // I am getting error on this
line.
deNewVDir.Properties["Path"].Insert(0,"c:\\TestVirDir"); // Just for
example.
deNewVDir.CommitChanges();
deRoot.CommitChanges();

// Create a Application
if (strSchema == "IIsWebVirtualDir")
deNewVDir.Invoke("AppCreate",true);

// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
Response.Write("Virtual Directory TestVirDir has been created");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}

Interestingly Same code logic works when I create windows application. It
seems it is not allowing me to create Virtual Dir through web-based
application(ASP.NET & C#) because IIS is in use and this is why access is
denied error comes. So what could be the solution for this ?

TIA
Jaydeep


Lionel LASKE said:
This requirement is just for "enumerate properties". If you already know
wich properties you need, you don't have to enumerate properties.

Just in case: Are you sure you add to your project a reference to
"System.DirectoryServices.dll" ?
http://msdn.microsoft.com/library/d...html/ccfc5710-93ae-474e-be09-6c7250629909.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top