- 1 // 返回一串字符
- 2 GO
- 3
- 4 /****** Object: StoredProcedure [dbo].[proc_LoginOutPut] Script Date: 9/23/2019 1:04:29 PM ******/
- 5 SET ANSI_NULLS ON
- 6 GO
- 7
- 8 SET QUOTED_IDENTIFIER ON
- 9 GO
- 10
- 11
- 12 -- =============================================
- 13 -- Author: <Author,,Name>
- 14 -- Create date: <2019-04-25 15:00:00,>
- 15 -- Description: <登录的方法>
- 16 -- 查询用户名是否存在,
- 17 -- 不存在:
- 18 -- 返回: 用户名或密码错误 请检查。
- 19 -- 存在:
- 20 -- 判断用户名和密码是否匹配
- 21 -- 匹配,看连续密码输入次数是否>0<5
- 22 -- 是,清除次数, 直接登录获取更详细信息———————— 返回
- 23 -- 否:看解锁时间是否大于等于当前时间(是:清除解锁时间、清除次数、改状态0),返回详细信息
- 24 -- (否:返回,您当前处于锁定状态,请在XX时间后进行登录 )
- 25 -- 不匹配:
- 26 -- 根据account 查找id给该用户加一次锁定次数,判断有没有到5次,有:更改锁定状态和解锁时间
- 27 -- 没有:返回您输入的账号或密码错误
- 28
- 29 -- =============================================
- 30
- 31
- 32 ALTER PROCEDURE [dbo].[proc_LoginOutPut]
- 33 @Account varchar(20), --账号
- 34 @Pwd varchar(50), --密码
- 35 @strOutput VARCHAR(100) output --输出内容
- 36
- 37 --输出格式:0~由于您最近输错5次密码已被锁定,请在XX之后再尝试登录~id。 id 不存在写0.存在写自己id
- 38 --0~用户名或密码错误~id。
- 39 -- 1~id~id
- 40 -- -1~发生错误~id
- 41 -- -1~发生错误 0不成功 1 登录成功
- 42 AS
- 43
- 44 BEGIN
- 45 SET XACT_ABORT ON--如果出错,会将transcation设置为uncommittable状态
- 46 declare @PasswordIncorrectNumber int --连续密码输入次数
- 47 declare @Id int --用户id
- 48 declare @count int --用户匹配行数
- 49 declare @UnLockTime datetime --解锁时间
- 50
- 51 BEGIN TRANSACTION
- 52 -- 开始逻辑判断
- 53
- 54 ----------非空判断
- 55 if(@Account = '' or @Account is null or @Pwd='' or @Pwd is null)
- 56
- 57 begin
- 58 set @strOutput='0~未获取到信息,请稍后重试~0'
- 59 return @strOutput
- 60 end
- 61 ----------非空判断结束
- 62
- 63
- 64 else
- 65 begin
- 66 set @Id=(select id from t_user where Account=@Account or AdAccount=@Account)
- 67 -- 1:查询用户名是否存在
- 68 if @Id>0--说明账号存在
- 69 begin
- 70 set @count=(select count(id) from t_user where (Account=@Account and Pwd=@Pwd) or (AdAccount=@Account and Pwd=@Pwd))
- 71 if @count=1
- 72 begin
- 73 set @PasswordIncorrectNumber=(select PasswordIncorrectNumber from t_user where id=@Id)
- 74 --看连续密码输入次数是否>0 <5
- 75 if @PasswordIncorrectNumber<5
- 76 begin
- 77 --清除次数, 直接登录获取更详细信息———————— 返回
- 78 update t_user set PasswordIncorrectNumber=0 ,UnLockTime=null ,State=0
- 79 from t_user where id=@Id
- 80 set @strOutput= '1~'+ '登录成功'+'~'+CAST(@Id AS NVARCHAR(10))
- 81
- 82 select CAST(@strOutput AS NVARCHAR(20))
- 83
- 84
- 85
- 86
- 87 end
- 88 else --次数大于5,已经被锁住
- 89 begin
- 90 -- 看解锁时间是否大于等于当前时间(是:清除解锁时间、清除次数、改状态0),返回详细信息
- 91 set @UnLockTime=(select [UnLockTime] from t_user where id=@Id)
- 92 if @UnLockTime>GETDATE()
- 93 begin
- 94 set @strOutput='0~由于您最近输错5次密码已被锁定,请在'+CONVERT(varchar(100), @UnLockTime, 20) +'之后再尝试登录~'+CAST(@Id AS NVARCHAR(10))
- 95 -- select @strOutput
- 96 end
- 97 else --清除解锁时间、清除次数、改状态0
- 98 begin
- 99 update t_user set PasswordIncorrectNumber=0 ,State=0,UnLockTime=null
- 100 from t_user where id=@Id
- 101 set @strOutput= '1~'+ '登录成功'+'~'+CAST(@Id AS NVARCHAR(10))
- 102 select @strOutput
- 103 end
- 104 end
- 105
- 106 end
- 107 else -- 账号和密码不匹配,但是属于我们系统用户 。
- 108 begin
- 109 -- 根据id给该用户加一次锁定次数,判断有没有到5次,有:更改锁定状态和解锁时间
- 110 update t_user set PasswordIncorrectNumber=PasswordIncorrectNumber+1
- 111 from t_user where id=@Id
- 112 set @PasswordIncorrectNumber=(select PasswordIncorrectNumber from t_user where id=@Id)
- 113 if @PasswordIncorrectNumber>4
- 114 begin
- 115 set @UnLockTime=(select dateadd(MINUTE,30,GETDATE() ))--UnLockTime 往后加半个小时 CONVERT(varchar(100), @UnLockTime, 20)
- 116 update t_user set State=1,UnLockTime=@UnLockTime
- 117 from t_user where id=@Id -- State=1锁定,
- 118
- 119 INSERT INTO t_user_Log (pId , Account , AdAccount , Pwd , Name , DepId , RoleId , Email , Tel , State , PasswordIncorrectNumber , UnLockTime , CreateUserId , NextUpdatePwdTime)
- 120 SELECT @Id,Account , AdAccount , Pwd , Name , DepId , RoleId , Email , Tel , State , PasswordIncorrectNumber , UnLockTime , CreateUserId , NextUpdatePwdTime
- 121 FROM t_user WHERE t_user.Id=@Id
- 122
- 123
- 124
- 125 set @UnLockTime= CONVERT(varchar(100), @UnLockTime, 20)
- 126 set @strOutput='0~由于您最近输错5次密码已被锁定,请在'+CONVERT(varchar(100), @UnLockTime, 20) +'之后再尝试登录~'+CAST(@Id AS NVARCHAR(10))
- 127 select @strOutput
- 128 end
- 129 else --
- 130 begin
- 131
- 132 set @strOutput='0~用户名或密码错误'+'~'+CAST(@Id AS NVARCHAR(10))
- 133 select @strOutput
- 134 end
- 135 end
- 136 end
- 137 else --不存在 返回: 2~不是我们用户,不用加登录日志。
- 138 begin
- 139 set @strOutput='2~不是我们用户,不用加登录日志'+'~0'
- 140 select @strOutput
- 141 end
- 142 end
- 143
- 144 IF @@error <> 0 --发生错误
- 145
- 146 BEGIN
- 147
- 148 ROLLBACK TRANSACTION
- 149 set @strOutput='-1~发生错误~0'
- 150
- 151 SELECT @strOutput
- 152
- 153 END
- 154
- 155 ELSE
- 156
- 157 BEGIN
- 158
- 159 COMMIT TRANSACTION
- 160
- 161 --执行成功 RETURN 1
- 162
- 163 SELECT @strOutput
- 164 END
- 165 END
- 166 GO
- 167
- 168
- 169 //调用
- 170
- 171 /// <summary>
- 172 /// 检验用户账号
- 173 /// </summary>
- 174 /// <param name="user"></param>
- 175 /// <returns></returns>
- 176 public static string CheckUser(EnUser user)
- 177 {
- 178
- 179 string sql = string.Format("proc_LoginOutPut");
- 180
- 181 List<KeyValue> paralist = new List<KeyValue>();
- 182 paralist.Add(new KeyValue { Key = "@Account", Value = user.Account });
- 183 paralist.Add(new KeyValue { Key = "@Pwd", Value = user.Pwd });
- 184 object Objreturn = SQLHelper.RunProcedureForObject(sql, "strOutput", paralist);
- 185 String returnStr = "";
- 186 if (Objreturn != null)
- 187 {
- 188 returnStr = Objreturn.ToString();
- 189
- 190 }
- 191 if (returnStr.Length > 0)
- 192 {
- 193 return returnStr;
- 194
- 195 }
- 196 else
- 197 {
- 198 return "";
- 199 }
- 200 }
- 201
- 202 //sqlhelper
- 203
- 204 /// <summary>
- 205 /// 带参数执行存储过程并返回指定参数
- 206 /// </summary>
- 207 /// <param name="str_conn">数据库链接名称</param>
- 208 /// <param name="str_sql">SQL脚本</param>
- 209 /// <param name="str_returnName">返回值的变量名</param>
- 210 /// <param name="ilst_params">参数列表</param>
- 211 /// <returns>存储过程返回的参数</returns>
- 212 public static object RunProcedureForObject( string str_sql, string str_returnName, IList<KeyValue> ilst_params)
- 213 {
- 214 using (SqlConnection sqlCon = new SqlConnection(connectionString))
- 215 {
- 216 sqlCon.Open();
- 217 SqlCommand sqlCmd = sqlCon.CreateCommand();
- 218 sqlCmd.CommandType = CommandType.StoredProcedure;
- 219 sqlCmd.CommandText = str_sql;
- 220 FillPram(sqlCmd.Parameters, ilst_params);
- 221 //添加返回值参数
- 222 SqlParameter param_outValue = new SqlParameter(str_returnName, SqlDbType.VarChar, 100);
- 223 param_outValue.Direction = ParameterDirection.InputOutput;
- 224 param_outValue.Value = string.Empty;
- 225 sqlCmd.Parameters.Add(param_outValue);
- 226 //执行存储过程
- 227 sqlCmd.ExecuteNonQuery();
- 228 //获得存过过程执行后的返回值
- 229 return param_outValue.Value;
- 230 }
- 231 }