如何获取服务器域名
谁能告诉我如何在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要从请求对象中提取它:
You'll need to extract it from the request object:
如果这里有人实际上正在搜索服务器的域名,那么这是我自 .Net 诞生以来一直在使用的一个技巧:
您实际上可以从那里获取一些信息。更新:现在适用于 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:
You can actually grab a bit of info from there. Update: Now works with 64 bit.
Thanks Duncan for the 64bit version.
有点晚了..但是在遇到完全相同的问题后,我发现了缺失的最佳答案:
如msdn 站点:
在我的个人电脑(无 AD 域)和 IIS(匿名访问)下的工作(有 AD 域)上进行了测试。
A bit late.. But the missing and best answer I have found, after having exactly the same issue:
As stated on the msdn site:
Tested on my personal pc (no AD-domain) and at work (with AD-domain) under IIS (anonymous access).
在我看来,您正在尝试查找用户的域名。由于您要求提供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.
您的问题存在具体问题,某一特定 IP 地址可能有多个域名。
正如 Tinister 所说,您可以使用
但这只会告诉您用户在浏览器地址栏中写的内容。如果用户在您站点的主机文件中添加了一个条目,然后使用该主机名,您将看到这样的内容。 (我不知道他们为什么要这样做)。
如果您的网站有多个域名,您可以使用它来确定用户请求的是哪个域名。
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
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.
尝试 System.Net.Dns 类,它有很多有用的方法,例如 GetHostEntry 即:
Try the
System.Net.Dns
class, it has plenty of helpful methods such as GetHostEntry i.e.:根据术士的回答:
如何获取网站的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);
在 web.config 中你需要添加这段代码
in the web.config you need to add the piece of code