Requirements: The internal Web site a menu allows only the specified domain user access, can not use the login window, similar to the user name password such things.
Solution: The user clicks the menu, obtains the IP, uses the NBTSTAT-A IP to obtain the client's computer name, in the backstage program filters out the computer name to judge.
Here are some of the code and workarounds that are important, the details are not the point, the focus is the idea, and of course you can write the code more beautifully and efficiently, ^^.
You can create a new page and write the following code in the background.
////////////////////////////////////////////////////////////
if (! IsPostBack)
{
String strclienthostname = GetHostName (GetIP ());
if (Strclienthostname = = "China-2021-k90" | | | strclienthostname = = "China-hjbai")
{
Bind (); Output results
}
Else
{
Page.ClientScript.RegisterStartupScript (this. GetType (), "Warning", "" ");
}
}
////////////////////////////////////////////////////////////
Get Client IP
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 ();
Smart here, you'll think of a lot of interesting uses for 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;
This is the result of the regular filter, you can run a DOS command in the cmd window to see the results, it will be more clear
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 method of stitching, because some computer names are more special
String Re1 = "(in)"; 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 "";
}
}
}
}
//////////////////////////////////////////////////////////////