写了个小程序用于asp.net系统的用户将自己的帐号绑定到特定的ip上面,这样子其他ip的电脑不能登陆,当然ip是可以改的,可以伪造,没有实际意思(这个只是一个demo),如果有需要还可以绑定到mac地址,主机名等等。。,顺便也记录了登录日志;
思路很简单:
1,设计一个存放绑定信息的表如下
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
CREATE
TABLE
[
dbo
]
.
[
SYS_Security_BindingLocking
]
(
[ Guid ] [ uniqueidentifier ] NOT NULL DEFAULT ( newid ()),
[ Id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ BindingLocking_Account ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--帐号
[ BindingLocking_IP ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的ip
[ BindingLocking_MAC ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的mac,预留
[ BindingLocking_Computer ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的主机名,预留
[ BindingLocking_Remark ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ BindingLocking_LoginErrorCount ] [ int ] NULL CONSTRAINT [ DF_SYS_Security_BindingLocking_BindingLocking_LoginErrorCount ] DEFAULT (( 0 )),--出错次数,用于达到错误次数后锁定帐号,预留
[ Active ] [ int ] NULL ,
[ CreateTime ] [ datetime ] NULL DEFAULT ( getdate ()),
[ Creater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ CreaterDeptId ] [ int ] NULL ,
[ UpdateTime ] [ datetime ] NULL ,
[ Updater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ DateTime ] [ timestamp ] NULL
) ON [ PRIMARY ]
[ Guid ] [ uniqueidentifier ] NOT NULL DEFAULT ( newid ()),
[ Id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ BindingLocking_Account ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--帐号
[ BindingLocking_IP ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的ip
[ BindingLocking_MAC ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的mac,预留
[ BindingLocking_Computer ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,--绑定的主机名,预留
[ BindingLocking_Remark ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ BindingLocking_LoginErrorCount ] [ int ] NULL CONSTRAINT [ DF_SYS_Security_BindingLocking_BindingLocking_LoginErrorCount ] DEFAULT (( 0 )),--出错次数,用于达到错误次数后锁定帐号,预留
[ Active ] [ int ] NULL ,
[ CreateTime ] [ datetime ] NULL DEFAULT ( getdate ()),
[ Creater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ CreaterDeptId ] [ int ] NULL ,
[ UpdateTime ] [ datetime ] NULL ,
[ Updater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ DateTime ] [ timestamp ] NULL
) ON [ PRIMARY ]
2,用户登录时获取本机的ip地址在上表中去匹配,如果相同则通过,否则不通过
获取ip地址的方法:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 获取客户端ip
/// </summary>
/// <returns></returns>
public static string GetClientIP(HttpContext content)
{
// 可以透过代理服务器
string userIP = content.Request.ServerVariables[ " HTTP_X_FORWARDED_FOR " ];
if (userIP == null || userIP == "" )
{
// 没有代理服务器,如果有代理服务器获取的是代理服务器的IP
userIP = content.Request.ServerVariables[ " REMOTE_ADDR " ];
}
return userIP;
}
/// 获取客户端ip
/// </summary>
/// <returns></returns>
public static string GetClientIP(HttpContext content)
{
// 可以透过代理服务器
string userIP = content.Request.ServerVariables[ " HTTP_X_FORWARDED_FOR " ];
if (userIP == null || userIP == "" )
{
// 没有代理服务器,如果有代理服务器获取的是代理服务器的IP
userIP = content.Request.ServerVariables[ " REMOTE_ADDR " ];
}
return userIP;
}
登录事件以及写日志方法如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 登录事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void loginbtn_Click( object sender, EventArgs e)
{
if ( string .IsNullOrEmpty( this .username.Text.Trim()))
{
Response.Write( " <script>alert('用户名不能为空!')</script> " );
}
Session[ " username " ] = this .username.Text.Trim();
string sSQLTemp = " select BindingLocking_IP, BindingLocking_MAC,BindingLocking_Computer from SYS_Security_BindingLocking where BindingLocking_Account =' " + this .username.Text.Trim() + " ' " ;
DataTable dtTable = DbHelperSQL.Query(sSQLTemp).Tables[ 0 ];
string strIP = "" ;
if (dtTable.Rows.Count > 0 )
{
strIP = dtTable.Rows[ 0 ][ " BindingLocking_IP " ].ToString();
}
if (strIP.Length > 0 && strIP != this .ip.Text.Trim())
{
string strIPTemp = Common.GetClientIP(HttpContext.Current);
SetLogToSecurity( this .username.Text.Trim(), this .ip.Text.Trim(), " 失败 " , " 登陆的IP跟自己设置绑定的IP不一致 " ); // 写入日志
Response.Write( " <script>alert('登陆的IP跟自己设置绑定的IP不一致,请联系管理员设置!')</script> " );
return ;
}
else
{
SetLogToSecurity( this .username.Text.Trim(), this .ip.Text.Trim(), " 成功 " , " 成功 " ); // 写入日志
Response.Redirect( " Setting.aspx " ); // 直接进入设置页面
}
}
/// <summary>
/// 功能描述:写入SYS_Security_LoginInfo登陆信息表
/// </summary>
/// <param name="strAccount"></param>
/// <param name="strIP"></param>
/// <param name="strMac"></param>
/// <param name="strComp"></param>
/// <param name="IsLoginOK"></param>
/// <param name="strAction"></param>
private void SetLogToSecurity( string strAccount, string strIP, string IsLoginOK, string strAction)
{
string strSQL = " INSERT INTO [SYS_Security_LoginInfo]([LoginInfo_Account] ,[LoginInfo_IP],[LoginInfo_Time] ,[LoginInfo_Success] ,[LoginInfo_Action])VALUES(' " + strAccount + " ',' " + strIP + " ',' " + DateTime.Now.ToLocalTime() + " ',' " + IsLoginOK + " ',' " + strAction + " ') " ;
DbHelperSQL.ExecuteSql(strSQL);
}
/// 登录事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void loginbtn_Click( object sender, EventArgs e)
{
if ( string .IsNullOrEmpty( this .username.Text.Trim()))
{
Response.Write( " <script>alert('用户名不能为空!')</script> " );
}
Session[ " username " ] = this .username.Text.Trim();
string sSQLTemp = " select BindingLocking_IP, BindingLocking_MAC,BindingLocking_Computer from SYS_Security_BindingLocking where BindingLocking_Account =' " + this .username.Text.Trim() + " ' " ;
DataTable dtTable = DbHelperSQL.Query(sSQLTemp).Tables[ 0 ];
string strIP = "" ;
if (dtTable.Rows.Count > 0 )
{
strIP = dtTable.Rows[ 0 ][ " BindingLocking_IP " ].ToString();
}
if (strIP.Length > 0 && strIP != this .ip.Text.Trim())
{
string strIPTemp = Common.GetClientIP(HttpContext.Current);
SetLogToSecurity( this .username.Text.Trim(), this .ip.Text.Trim(), " 失败 " , " 登陆的IP跟自己设置绑定的IP不一致 " ); // 写入日志
Response.Write( " <script>alert('登陆的IP跟自己设置绑定的IP不一致,请联系管理员设置!')</script> " );
return ;
}
else
{
SetLogToSecurity( this .username.Text.Trim(), this .ip.Text.Trim(), " 成功 " , " 成功 " ); // 写入日志
Response.Redirect( " Setting.aspx " ); // 直接进入设置页面
}
}
/// <summary>
/// 功能描述:写入SYS_Security_LoginInfo登陆信息表
/// </summary>
/// <param name="strAccount"></param>
/// <param name="strIP"></param>
/// <param name="strMac"></param>
/// <param name="strComp"></param>
/// <param name="IsLoginOK"></param>
/// <param name="strAction"></param>
private void SetLogToSecurity( string strAccount, string strIP, string IsLoginOK, string strAction)
{
string strSQL = " INSERT INTO [SYS_Security_LoginInfo]([LoginInfo_Account] ,[LoginInfo_IP],[LoginInfo_Time] ,[LoginInfo_Success] ,[LoginInfo_Action])VALUES(' " + strAccount + " ',' " + strIP + " ',' " + DateTime.Now.ToLocalTime() + " ',' " + IsLoginOK + " ',' " + strAction + " ') " ;
DbHelperSQL.ExecuteSql(strSQL);
}
日志表:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
CREATE
TABLE
[
dbo
]
.
[
SYS_Security_LoginInfo
]
(
[ Guid ] [ uniqueidentifier ] NOT NULL CONSTRAINT [ DF__SYS_Securi__Guid__10CB707D ] DEFAULT ( newid ()),
[ Id ] [ bigint ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ LoginInfo_Account ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_IP ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_MAC ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Computer ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Time ] [ datetime ] NULL ,
[ LoginInfo_Success ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Action ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_UnlockAccount ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Remark ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ Active ] [ int ] NULL ,
[ CreateTime ] [ datetime ] NULL CONSTRAINT [ DF__SYS_Secur__Creat__11BF94B6 ] DEFAULT ( getdate ()),
[ Creater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ CreaterDeptId ] [ int ] NULL ,
[ UpdateTime ] [ datetime ] NULL ,
[ Updater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ DateTime ] [ timestamp ] NULL
) ON [ PRIMARY ]
[ Guid ] [ uniqueidentifier ] NOT NULL CONSTRAINT [ DF__SYS_Securi__Guid__10CB707D ] DEFAULT ( newid ()),
[ Id ] [ bigint ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ LoginInfo_Account ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_IP ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_MAC ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Computer ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Time ] [ datetime ] NULL ,
[ LoginInfo_Success ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Action ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_UnlockAccount ] [ varchar ] ( 100 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ LoginInfo_Remark ] [ varchar ] ( 200 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ Active ] [ int ] NULL ,
[ CreateTime ] [ datetime ] NULL CONSTRAINT [ DF__SYS_Secur__Creat__11BF94B6 ] DEFAULT ( getdate ()),
[ Creater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ CreaterDeptId ] [ int ] NULL ,
[ UpdateTime ] [ datetime ] NULL ,
[ Updater ] [ varchar ] ( 80 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ DateTime ] [ timestamp ] NULL
) ON [ PRIMARY ]
绑定ip方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 绑定ip的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void set_Click( object sender, EventArgs e)
{
string strSQLTemp = " select BindingLocking_Account from SYS_Security_BindingLocking where BindingLocking_Account =' " + LOGIN_Account + " ' " ;
DataTable dtTable = DbHelperSQL.Query(strSQLTemp).Tables[ 0 ];
string strGetIPAddr = "" ;
strGetIPAddr = Common.GetClientIP(HttpContext.Current); // 获取ip
if (dtTable.Rows.Count == 0 ) // 如果不存在
{
// 插入一条
string strIP = Common.GetClientIP(HttpContext.Current);
string strSQL = " insert into [SYS_Security_BindingLocking]([BindingLocking_Account], [BindingLocking_IP]) values(' " + LOGIN_Account + " ',' " + strGetIPAddr + " ') " ;
int ttt = DbHelperSQL.ExecuteSql(strSQL);
}
else
{
// 更新一条记录
string strSQL = " update [SYS_Security_BindingLocking] set [BindingLocking_IP] =' " + strGetIPAddr + " ' where [BindingLocking_Account] =' " + LOGIN_Account + " ' " ;
int ttt = DbHelperSQL.ExecuteSql(strSQL);
}
}
/// 绑定ip的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void set_Click( object sender, EventArgs e)
{
string strSQLTemp = " select BindingLocking_Account from SYS_Security_BindingLocking where BindingLocking_Account =' " + LOGIN_Account + " ' " ;
DataTable dtTable = DbHelperSQL.Query(strSQLTemp).Tables[ 0 ];
string strGetIPAddr = "" ;
strGetIPAddr = Common.GetClientIP(HttpContext.Current); // 获取ip
if (dtTable.Rows.Count == 0 ) // 如果不存在
{
// 插入一条
string strIP = Common.GetClientIP(HttpContext.Current);
string strSQL = " insert into [SYS_Security_BindingLocking]([BindingLocking_Account], [BindingLocking_IP]) values(' " + LOGIN_Account + " ',' " + strGetIPAddr + " ') " ;
int ttt = DbHelperSQL.ExecuteSql(strSQL);
}
else
{
// 更新一条记录
string strSQL = " update [SYS_Security_BindingLocking] set [BindingLocking_IP] =' " + strGetIPAddr + " ' where [BindingLocking_Account] =' " + LOGIN_Account + " ' " ;
int ttt = DbHelperSQL.ExecuteSql(strSQL);
}
}
/Files/heaiping/BindIP.rar