简体   繁体   中英

DirectoryEntry.Bind Works in vb.net but fails in c#

I am updating a web site that was written a while ago in vb.net. This web site does some basic administration on remote websites, like recycling an app pool or stopping a site instance. It is used mainly to list the sites on an IIS server with basic information.

I decided to rewrite it in C# as that is my daily language of choice.

After much trial and error, I have boiled the issue down to some very simple code. The site was working somewhat in vb.net. When I ported it to c# it stopped being able to access the remote IIS DirectoryEntry. If failed with a COM Exception or Access Denied. I tried it on different servers and different versions of .NET (2.0 and 4.5).

What I have now is 2 solutions in Visual Studio 2015. They both target .NET Framework 2.0. Both were created from the Empty ASP.NET Website template. After creation, I added a reference to the 2.0 .NET assembly System.DirectoryServices. I then added a single web form named default.aspx. I added a literal control to the aspx page and then opened the code behind file and added the following:

default.aspx.cs:

using System;
using System.DirectoryServices;
namespace CSWebInfo
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var result = String.Empty;
            DirectoryEntry W3SVC = new DirectoryEntry("IIS://dev_websrv_01/W3SVC", "domain\admin username", "admin password");
            foreach (DirectoryEntry s in W3SVC.Children)
            {
                result += s.Properties["ServerComment"][0].ToString() + "<br />";
            }
            LiteralDisplay.Text = result;
        }
    }
}

default.aspx.vb:

Imports System.DirectoryServices
Public Class _default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim result As String = ""
        Dim W3SVC As New DirectoryEntry("IIS://dev_websrv_01/W3SVC", "domain\admin username", "admin password") 
        For Each s As DirectoryEntry In W3SVC.Children
            result += s.Properties("ServerComment").Value + "<br />"
        Next        
        LiteralDisplay.Text = result
    End Sub
End Class

I didn't touch anything else. web.config files remain just as they were in the template.

This should list the friendly names for each site on the dev_websrv_01 server.

The vb.net site works perfectly. I get a simple list of each website. The c# site fails immediately upon access of W3SVC.Children in the foreach statement. The exception thrown is a COMException: Access is Denied.

Both sites are run straight from Visual Studio, using IISExpress on my desktop machine connected physically to the same network domain as the server.

Any ideas on why the exact same code in vb.net can access the remote server but the c# code cannot?

Thank you for any help!

 new DirectoryEntry("IIS://dev_websrv_01/W3SVC", "domain\admin username", "admin password");

This is your problem. You are using the backslash in domain\\admin , and it is interpreted as an escape sequence.

Either use this string literals:

@"domain\admin username"

Or these

"domain\\admin username"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM