內部網站特定選單僅允許指定網域使用者存取,不想顯示登入畫面,需自動辨識使用者身分。
nbtstat -A <IP> 取得 NetBIOS 名稱。public string GetIP()
{
string uip = "";
if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
uip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
else
uip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return uip;
}
public string GetHostname(string IP)
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "nbtstat",
Arguments = "-A " + IP,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process proc = Process.Start(psi))
{
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
output = output.Replace("\r", "").Replace("\n", "").Replace("\t", "").ToLower();
// 模式一:china-xxxx 格式
Regex reg = new Regex(@"china-(?:[a-z][a-z0-9_]*)*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match mc = reg.Match(output);
if (mc.Success) return mc.Value;
// 模式二:china-數字-xxxx 格式
string pattern = @"(china)([-+]\d+)(-)((?:[a-z][a-z0-9_]*))";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mc2 = r.Match(output);
if (mc2.Success) return mc2.Value;
// 找不到主機
if (Regex.IsMatch(output, "host not found", RegexOptions.IgnoreCase))
return "Host not found!";
return "";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string hostname = GetHostname(GetIP());
if (hostname == "china-2021-k90" || hostname == "china-hjbai")
{
BindData(); // 允許存取
}
else
{
ClientScript.RegisterStartupScript(GetType(), "Warning", "alert('存取被拒絕');", true);
}
}
}
nbtstat 僅在啟用 NetBIOS over TCP/IP 的 Windows 環境有效。