SQL Server 存储过程-验证用户是否存在

此存储过程旨在验证用户是否存在、密码错误、是否是在受限制的时间内以及当登入时在规定的时间内输入三次错误就限制登入时间

---用户登录系统
CREATE proc [dbo].[UserLogin]
(
   @UserAccount nvarchar(20),  ---登录账户
   @UserPassWord nvarchar(50), ---登录密码
   @IPAddress nvarchar(20),    ---主机IP地址
   @MacAddress nvarchar(20)    ---主机MAC地址
)
as
begin
   declare @LimitBeginDate datetime
   declare @PassWord nvarchar(20)
   declare @IsLogin nvarchar(1) ---0成功,1用户不存在,2密码错误,3受限期内
   set @PassWord=''
   select @PassWord=UserPassWord,@LimitBeginDate=LimitBeginDate from SysUser where UserAccount=@UserAccount
   if @PassWord=''
   begin
     ---用户帐户不存在
     set @IsLogin='1'
   end
   else
   begin
     ---用户帐户存在
     if @LimitBeginDate is not null
     begin
       ---存在限制登录时间
       if dateadd(hh,6,@LimitBeginDate)>getdate()
       begin
         ---用户帐户仍然在受限时间内
         set @IsLogin='3'
       end
       else
       begin
         ---用户帐户已经超过受限时间
         update SysUser set LimitBeginDate=null where UserAccount=@UserAccount
         if @PassWord=@UserPassWord
         begin
           ---用户密码正确
           set @IsLogin='0'
         end
         else
         begin
           ---用户密码错误
           set @IsLogin='2'
         end
       end
     end
     else
     begin
       ---不存在受限时间的
       if @PassWord=@UserPassWord
       begin
         ---用户密码正确
         set @IsLogin='0'
       end
       else
       begin
         --用户密码错误
         set @IsLogin='2'
       end
     end
   end
   ---其他操作
   insert into SysUserLog(UserAccount,UserPassWord,IPAddress,MacAddress,CreateDate,IsLogin)
        values(@UserAccount,@UserPassWord,@IPAddress,@MacAddress,getdate(),@IsLogin)
   if @@error=0
   begin
     ---查询最近这个用户最近三条数据,如果在15分钟内,连续输入错误密码3次,则修改用户表中LimitBeginDate字段
     if @IsLogin='2'
     begin
       declare @strTemp nvarchar(20)
       set @strTemp=''
       select top 3 @strTemp=@strTemp+IsLogin from SysUserLog where UserAccount=@UserAccount and CreateDate>dateadd(mi,-15,getdate()) order by CreateDate desc
       if @strTemp='222'
          update SysUser set LimitBeginDate=getdate() where UserAccount=@UserAccount
     end
     select @IsLogin as Flag
   end
   else
   begin
     ---验证错误
     select '4' as Flag
   end
end

O了再见

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值