如何获取服务器域名

发布于 2024-08-08 06:16:59 字数 176 浏览 2 评论 0原文

谁能告诉我如何在asp.net中获取服务器域名? (Environment.UserDomainName 返回“IIS APPPOOL”字符串)

感谢您的重播,但大多数都是关于服务器的 DNS 名称,我需要的是域名。例如,当我通过 Windows 身份验证登录时,我输入域\用户,我需要这个“域”

Can anybody tell me how to get servers domain name in asp.net? (Environment.UserDomainName returns "IIS APPPOOL" string)

Thanks for replays, but mostly they are about DNS name of the server, what I need is the domain name. For example when I login via windows authentication I type domain\user and I need this "domain"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

七颜 2024-08-15 06:16:59

您需要从请求对象中提取它:

HttpContext.Current.Request.Url.Host

You'll need to extract it from the request object:

HttpContext.Current.Request.Url.Host
内心激荡 2024-08-15 06:16:59

如果这里有人实际上正在搜索服务器的域名,那么这是我自 .Net 诞生以来一直在使用的一个技巧:

    [DllImport("netapi32.dll", CharSet = CharSet.Auto)]
    static extern int NetWkstaGetInfo(string server, int level, out IntPtr info);

    [DllImport("netapi32.dll")]
    static extern int NetApiBufferFree(IntPtr pBuf);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    class WKSTA_INFO_100
    {
        public int wki100_platform_id;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_computername;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_langroup;
        public int wki100_ver_major;
        public int wki100_ver_minor;
    }

    public static string GetMachineNetBiosDomain()
    {
        IntPtr pBuffer = IntPtr.Zero;

        WKSTA_INFO_100 info;
        int retval = NetWkstaGetInfo(null, 100, out pBuffer);
        if (retval != 0)
            throw new Win32Exception(retval);

        info = (WKSTA_INFO_100)Marshal.PtrToStructure(pBuffer, typeof(WKSTA_INFO_100));
        string domainName = info.wki100_langroup;
        NetApiBufferFree(pBuffer);
        return domainName;
    }

您实际上可以从那里获取一些信息。更新:现在适用于 64 位。

感谢 Duncan 提供 64 位版本。

If anyone here is actually searching for the domain name of the server, here is a hack I've been using since the .Net beginnings:

    [DllImport("netapi32.dll", CharSet = CharSet.Auto)]
    static extern int NetWkstaGetInfo(string server, int level, out IntPtr info);

    [DllImport("netapi32.dll")]
    static extern int NetApiBufferFree(IntPtr pBuf);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    class WKSTA_INFO_100
    {
        public int wki100_platform_id;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_computername;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string wki100_langroup;
        public int wki100_ver_major;
        public int wki100_ver_minor;
    }

    public static string GetMachineNetBiosDomain()
    {
        IntPtr pBuffer = IntPtr.Zero;

        WKSTA_INFO_100 info;
        int retval = NetWkstaGetInfo(null, 100, out pBuffer);
        if (retval != 0)
            throw new Win32Exception(retval);

        info = (WKSTA_INFO_100)Marshal.PtrToStructure(pBuffer, typeof(WKSTA_INFO_100));
        string domainName = info.wki100_langroup;
        NetApiBufferFree(pBuffer);
        return domainName;
    }

You can actually grab a bit of info from there. Update: Now works with 64 bit.

Thanks Duncan for the 64bit version.

怪我太投入 2024-08-15 06:16:59

有点晚了..但是在遇到完全相同的问题后,我发现了缺失的最佳答案:

private static string getComputerDomain()
        {
            try
            {
                return Domain.GetComputerDomain().Name;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                return "Local (No domain)";
            }
        }

如msdn 站点

此返回值与域凭据无关
应用程序运行。此方法将检索计算机的
域,无论其运行的受信任帐户域凭据如何
下。

在我的个人电脑(无 AD 域)和 IIS(匿名访问)下的工作(有 AD 域)上进行了测试。

A bit late.. But the missing and best answer I have found, after having exactly the same issue:

private static string getComputerDomain()
        {
            try
            {
                return Domain.GetComputerDomain().Name;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                return "Local (No domain)";
            }
        }

As stated on the msdn site:

This return value is independent of the domain credentials under which
the application is run. This method will retrieve the computer's
domain regardless of the trusted account domain credentials it is run
under.

Tested on my personal pc (no AD-domain) and at work (with AD-domain) under IIS (anonymous access).

浅紫色的梦幻 2024-08-15 06:16:59

在我看来,您正在尝试查找用户的域名。由于您要求提供Environment.UserDomainName。由于您的网站可能以“允许匿名访问”的方式运行 - 用户不会将其域信息传递到服务器,并且 IIS 会向您提供它所拥有的帐户信息,即应用程序池帐户。

It looks to me like you are trying to find the user's domain name. Since you are asking for the Environment.UserDomainName. Since your site is likely running with "Allow Anonymous Access" - the user isn't passing their domain information to the server and IIS is giving you the account information it does have, namely the app pool account.

荆棘i 2024-08-15 06:16:59

您的问题存在具体问题,某一特定 IP 地址可能有多个域名。

正如 Tinister 所说,您可以使用

HttpContext.Current.Request.Url.Host

但这只会告诉您用户在浏览器地址栏中写的内容。如果用户在您站点的主机文件中添加了一个条目,然后使用该主机名,您将看到这样的内容。 (我不知道他们为什么要这样做)。

如果您的网站有多个域名,您可以使用它来确定用户请求的是哪个域名。

There is specific problem to your question, there may be more than one domain name for a specific IP address.

As Tinister stated, you can use

HttpContext.Current.Request.Url.Host

But that will only tell you what the user wrote in the address bar of the browser. If the user added an entry to their host file for your site, and then use that host name, that is what you will see. (I have no idea why they would do so).

If you have more than one domain name for your web site, you can use that to figure out which of the domain names that the user requested.

梦里的微风 2024-08-15 06:16:59

尝试 System.Net.Dns 类,它有很多有用的方法,例如 GetHostEntry 即:

var entry = System.Net.Dns.GetHostEntry("google.com"); // or vice-versa...
var name = System.Net.Dns.GetHostEntry("127.0.0.1"); // localhost ;)

Try the System.Net.Dns class, it has plenty of helpful methods such as GetHostEntry i.e.:

var entry = System.Net.Dns.GetHostEntry("google.com"); // or vice-versa...
var name = System.Net.Dns.GetHostEntry("127.0.0.1"); // localhost ;)
是伱的 2024-08-15 06:16:59

根据术士的回答:
如何获取网站的baseurl?

优雅:)

字符串baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

As per Warlock's answer:
How can I get the baseurl of site?

Elegant :)

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

小嗷兮 2024-08-15 06:16:59

在 web.config 中你需要添加这段代码

<authentication mode="Windows"/>
    <authorization>
      <deny users="?"/>
    </authorization>

    <identity impersonate="true"/>

in the web.config you need to add the piece of code

<authentication mode="Windows"/>
    <authorization>
      <deny users="?"/>
    </authorization>

    <identity impersonate="true"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文