. NET detects the user's computer name by retrieving the client IP address

Source: Internet
Author: User
Tags httpcontext servervariables

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 "";

}

}

}

}

//////////////////////////////////////////////////////////////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.