asp.net系統的使用者將自己的帳號繫結到特定的ip上面

iDotNetSpace發表於2010-01-14
寫了個小程式用於asp.net系統的使用者將自己的帳號繫結到特定的ip上面,這樣子其他ip的電腦不能登陸,當然ip是可以改的,可以偽造,沒有實際意思(這個只是一個demo),如果有需要還可以繫結到mac地址,主機名等等。。,順便也記錄了登入日誌;

思路很簡單:

1,設計一個存放繫結資訊的表如下

 
程式碼
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]

 

2,使用者登入時獲取本機的ip地址在上表中去匹配,如果相同則通過,否則不通過

獲取ip地址的方法:

 
程式碼
///
    /// 獲取客戶端ip
    ///

    ///
    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;
    }

 

 

登入事件以及寫日誌方法如下:

 
程式碼
///
    /// 登入事件
    ///

    ///
    ///
    protected void loginbtn_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.username.Text.Trim()))
        {
            Response.Write("
        }
        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("
            return;
        }
        else
        {
            SetLogToSecurity(this.username.Text.Trim(), this.ip.Text.Trim(), "成功", "成功"); // 寫入日誌

            Response.Redirect("Setting.aspx");//直接進入設定頁面
        }
    }

    ///
    /// 功能描述:寫入SYS_Security_LoginInfo登陸資訊表
    ///

    ///
    ///
    ///
    ///
    ///
    ///
    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);
    }

 

日誌表:

 
程式碼
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]

 

 

繫結ip方法

 
程式碼
///
    /// 繫結ip的方法
    ///

    ///
    ///
    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);
        }
    }

 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-625130/,如需轉載,請註明出處,否則將追究法律責任。

相關文章