简体   繁体   English

DirectoryEntry.Bind在vb.net中有效,但在c#中失败

[英]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. 我正在更新前一段时间在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. 它主要用于列出IIS服务器上具有基本信息的站点。

I decided to rewrite it in C# as that is my daily language of choice. 我决定用C#重写它,因为这是我每天的选择语言。

After much trial and error, I have boiled the issue down to some very simple code. 经过多次尝试和错误,我将问题简化为一些非常简单的代码。 The site was working somewhat in vb.net. 该网站在vb.net上工作正常。 When I ported it to c# it stopped being able to access the remote IIS DirectoryEntry. 当我将其移植到c#时,它停止能够访问远程IIS DirectoryEntry。 If failed with a COM Exception or Access Denied. 如果由于COM异常或访问被拒绝而失败。 I tried it on different servers and different versions of .NET (2.0 and 4.5). 我在不同的服务器和不同版本的.NET(2.0和4.5)上进行了尝试。

What I have now is 2 solutions in Visual Studio 2015. They both target .NET Framework 2.0. 我现在拥有的是Visual Studio 2015中的2个解决方案。它们都针对.NET Framework 2.0。 Both were created from the Empty ASP.NET Website template. 两者都是从Empty ASP.NET网站模板创建的。 After creation, I added a reference to the 2.0 .NET assembly System.DirectoryServices. 创建后,我添加了对2.0 .NET程序集System.DirectoryServices的引用。 I then added a single web form named default.aspx. 然后,我添加了一个名为default.aspx的Web表单。 I added a literal control to the aspx page and then opened the code behind file and added the following: 我在aspx页面上添加了文字控件,然后打开了文件后面的代码,并添加了以下内容:

default.aspx.cs: 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: 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. web.config文件与模板中的文件一样保留。

This should list the friendly names for each site on the dev_websrv_01 server. 这应该列出dev_websrv_01服务器上每个站点的友好名称。

The vb.net site works perfectly. vb.net网站运行良好。 I get a simple list of each website. 我得到每个网站的简单列表。 The c# site fails immediately upon access of W3SVC.Children in the foreach statement. 在foreach语句中访问W3SVC.Children后,c#站点立即失败。 The exception thrown is a COMException: Access is Denied. 引发的异常是COMException:访问被拒绝。

Both sites are run straight from Visual Studio, using IISExpress on my desktop machine connected physically to the same network domain as the server. 这两个站点都是从Visual Studio直接运行的,使用台式机上的IISExpress物理连接到与服务器相同的网络域。

Any ideas on why the exact same code in vb.net can access the remote server but the c# code cannot? 关于为什么vb.net中完全相同的代码可以访问远程服务器但c#代码不能访问的任何想法?

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. 您在domain\\admin中使用反斜杠,它被解释为转义序列。

Either use this string literals: 使用以下字符串文字:

@"domain\admin username"

Or these 或者这些

"domain\\admin username"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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