C# 如何获取服务器域名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1601162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get server domain name
提问by Sly
Can anybody tell me how to get servers domain name in asp.net? (Environment.UserDomainName
returns "IIS APPPOOL" string)
谁能告诉我如何在asp.net中获取服务器域名?(Environment.UserDomainName
返回“IIS APPPOOL”字符串)
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"
感谢重播,但主要是关于服务器的 DNS 名称,我需要的是域名。例如,当我通过 Windows 身份验证登录时,我输入 domain\user 并且我需要这个“域”
采纳答案by Tom B
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.
在我看来,您正在尝试查找用户的域名。由于您要求提供 Environment.UserDomainName。由于您的站点可能使用“允许匿名访问”运行 - 用户没有将他们的域信息传递给服务器,而 IIS 会向您提供它确实拥有的帐户信息,即应用程序池帐户。
回答by John Leidegren
Try the System.Net.Dns
class, it has plenty of helpful methods such as GetHostEntry i.e.:
试试这个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 ;)
回答by Tinister
You'll need to extract it from the request object:
您需要从请求对象中提取它:
HttpContext.Current.Request.Url.Host
回答by Pete
There is specific problem to your question, there may be more than one domain name for a specific IP address.
您的问题有特定问题,特定IP地址可能有多个域名。
As Tinister stated, you can use
正如 Tinister 所说,你可以使用
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.
如果您的网站有多个域名,您可以使用它来确定用户请求的是哪个域名。
回答by Martin Sansone - MiOEE
As per Warlock's answer: How can I get the baseurl of site?
根据术士的回答: 如何获取站点的 baseurl?
Elegant :)
优雅的 :)
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
字符串 baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
回答by TombMedia
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:
如果这里有人真的在搜索服务器的域名,这里是我从 .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;
}
You can actually grab a bit of info from there. Update: Now works with 64 bit.
您实际上可以从那里获取一些信息。更新:现在适用于 64 位。
回答by lokanath das
in the web.config you need to add the piece of code
在 web.config 你需要添加一段代码
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
<identity impersonate="true"/>
回答by Remco
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:
如 msdn站点所述:
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).
在我的个人电脑(无 AD 域)和 IIS 下的工作(有 AD 域)上测试(匿名访问)。