. Net checks the user's computer name by obtaining the Client IP address.

Source: Internet
Author: User

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

}

}

}

}

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

Related Article

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.