要求内部网站某一个菜单只允许规定的域用户访问不能用登录窗口类似用户名密码这样的东西
解决方法用户点击菜单得到IP用NBTSTAT A IP得到客户端的计算机名在后台程序中用正则过滤出计算机名进行判断
下面是部分代码和解决方法变通还是很重要的细节不是重点重点是想法当然你可以把代码写的更漂亮高效^^
你可以新建个页面在后台写上下面的代码
////////////////////////////////////////////////////////////
if (!IsPostBack)
{
string strClientHostname = GetHostname(GetIP());
if (strClientHostname == "chinak" || strClientHostname == "chinahjbai" )
{
bind(); //输出结果
}
else
{
PageClientScriptRegisterStartupScript(thisGetType() "Warning" "");
}
}
////////////////////////////////////////////////////////////
//得到客户端IP
public string GetIP()
{
string uip = "";
if (HttpContextCurrentRequestServerVariables["HTTP_VIA"] != null)
{
uip = HttpContextCurrentRequestServerVariables["HTTP_X_FORWARDED_FOR"]ToString();
}
else
{
uip = HttpContextCurrentRequestServerVariables["REMOTE_ADDR"]ToString();
}
return uip;
}
////////////////////////////////////////////////////////////
public string GetHostname(string IP)
{
string dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
//这里聪明的你会想到很多命令的有趣用法吧
//psiFileName = "ping ";
//psiRedirectStandardInput = false;
//psiRedirectStandardOutput = true;
//psiArguments = "a n " + IP;
psiFileName = "nbtstat ";
psiRedirectStandardInput = false;
psiRedirectStandardOutput = true;
psiArguments = "A " + IP;
//这里对结果进行正则过滤你可以在CMD窗口运行DOS命令看下结果这样会更明了
psiUseShellExecute = false;
proc = ProcessStart(psi);
dirResults = procStandardOutputReadToEnd();
procWaitForExit();
dirResults = dirResultsReplace("\r" "")Replace("\n" "")Replace("\t" "");
Regex reg = new Regex("china(?:[az][az_]*)*" RegexOptionsIgnoreCase | RegexOptionsCompiled);
dirResults = dirResultsToLower();
Match mc = regMatch(dirResults);
//ResponseWrite(dirResultsToLower());
if (mcSuccess)
{
return mcToString();
}
else
{
//这个是正则的另一种拼接方法因为有些计算机名比较特殊
string re = "(china)"; // Word
string re = "([+]\\d+)"; // Integer Number
string re = "()"; // Any Single Character
string re = "((?:[az][az_]*))"; // Variable Name
Regex r = new Regex(re + re + re + re RegexOptionsIgnoreCase | RegexOptionsSingleline);
Match mc = rMatch(dirResultsToLower());
if (mcSuccess)
{
return mcToString();
}
else
{
reg = new Regex("Host not found" RegexOptionsIgnoreCase | RegexOptionsCompiled);
mc = regMatch(dirResults);
if (mcSuccess)
{
return "Host not found!";
}
else
{
return "";
}
}
}
}
//////////////////////////////////////////////////////////////