Requirement: a menu of an internal website can only be accessed by specified domain users, but cannot be accessed using a logon window, such as a user name and password.
Solution: Click the menu to obtain the IP address. Use NBTSTAT-A to obtain the computer name of the client, and filter out the computer name using regular expressions in the background program for determination.
Below are some code and solutions. Flexibility is still very important. The details are not important, but the focus is on ideas. Of course, you can write code more beautifully and efficiently. ^.
You can create a new page and write the upper and lower code in the background.
//////////////////////////////////////// ////////////////////
If (! IsPostBack)
{
String strClientHostname = GetHostname (GetIP ());
If (strClientHostname = "china-2021-k90" | strClientHostname = "china-hjbai ")
{
Bind (); // output result
}
Else
{
Page. ClientScript. RegisterStartupScript (this. GetType (), "Warning ","");
}
}
//////////////////////////////////////// ////////////////////
// Obtain the Client IP Address
Public string GetIP ()
{
String uip = "";
If (HttpContext. Current. Request. ServerVariables ["HTTP_VIA"]! = Null)
{
Uip = HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]. ToString ();
}
Else
{
Uip = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"]. ToString ();
}
Return uip;
}
//////////////////////////////////////// ////////////////////
Public string GetHostname (string IP)
{
String dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo ();
Process proc = new Process ();
// You may think of interesting usage of many commands.
// Psi. FileName = "ping ";
// Psi. RedirectStandardInput = false;
// Psi. RedirectStandardOutput = true;
// Psi. Arguments = "-a-n 1" + IP;
Psi. FileName = "nbtstat ";
Psi. RedirectStandardInput = false;
Psi. RedirectStandardOutput = true;
Psi. Arguments = "-A" + IP;
// Perform regular filtering on the results here. You can run the doscommand In the CMD window to view the results, which will be clearer.
Psi. UseShellExecute = false;
Proc = Process. Start (psi );
DirResults = proc. StandardOutput. ReadToEnd ();
Proc. WaitForExit ();
DirResults = dirResults. Replace ("r", ""). Replace ("n", ""). Replace ("t ","");
Regex reg = new Regex ("china -(? : [A-z] [a-z0-9 _] *) * ", RegexOptions. IgnoreCase | RegexOptions. Compiled );
DirResults = dirResults. ToLower ();
Match mc = reg. Match (dirResults );
// Response. Write (dirResults. ToLower ());
If (mc. Success)
{
Return mc. ToString ();
}
Else
{
// This is another splicing method of regular expressions, because some computer names are special.
String re1 = "(china)"; // Word 1
String re2 = "([-+] d +)"; // Integer Number 1
String re3 = "(-)"; // Any Single Character 1
String re4 = "((? : [A-z] [a-z0-9 _] *) "; // Variable Name 1
Regex r = new Regex (re1 + re2 + re3 + re4, RegexOptions. IgnoreCase | RegexOptions. Singleline );
Match mc2 = r. Match (dirResults. ToLower ());
If (mc2.Success)
{
Return mc2.ToString ();
}
Else
{
Reg = new Regex ("Host not found", RegexOptions. IgnoreCase | RegexOptions. Compiled );
Mc = reg. Match (dirResults );
If (mc. Success)
{
Return "Host not found! ";
}
Else
{
Return "";
}
}
}
}
//////////////////////////////////////// //////////////////////