[英]How to get server domain name
任何人都可以告诉我如何在asp.net中获取服务器域名? ( Environment.UserDomainName
返回“IIS APPPOOL”字符串)
感谢重播,但主要是关于服务器的DNS名称,我需要的是域名。 例如,当我通过Windows身份验证登录时,我输入domain \\ user,我需要这个“域名”
您需要从请求对象中提取它:
HttpContext.Current.Request.Url.Host
如果这里的任何人实际上是在搜索服务器的域名,那么这是我自.Net开始以来一直在使用的hack:
[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位。
有点晚..但是在我遇到完全相同的问题后,我发现了缺失和最佳答案:
private static string getComputerDomain()
{
try
{
return Domain.GetComputerDomain().Name;
}
catch (ActiveDirectoryObjectNotFoundException)
{
return "Local (No domain)";
}
}
如msdn 网站上所述:
此返回值独立于运行应用程序的域凭据。 无论在其下运行的受信任帐户域凭据如何,此方法都将检索计算机的域。
在IIS(匿名访问)下测试我的个人电脑(没有AD域)和工作(使用AD域)。
在我看来,你正试图找到用户的域名。 因为您要求使用Environment.UserDomainName。 由于您的站点可能正在运行“允许匿名访问” - 用户未将其域信息传递给服务器,IIS正在向您提供其拥有的帐户信息,即应用程序池帐户。
您的问题存在特定问题,特定IP地址可能有多个域名。
正如Tinister所说,你可以使用
HttpContext.Current.Request.Url.Host
但这只会告诉您用户在浏览器的地址栏中写了什么。 如果用户在您的站点的主机文件中添加了一个条目,然后使用该主机名,那么您将看到该名称。 (我不知道他们为什么会这样做)。
如果您的网站有多个域名,则可以使用该域名来确定用户请求的域名。
试试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 ;)
在web.config中,您需要添加一段代码
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
<identity impersonate="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.