How to get server domain name
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"
You'll need to extract it from the request object:
HttpContext.Current.Request.Url.Host
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.
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).
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.
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.
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 ;)
As per Warlock's answer: How can I get the baseurl of site?
Elegant :)
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
in the web.config you need to add the piece of code
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
<identity impersonate="true"/>
精彩评论