How to Validate Logeo sql server in store procedure with input variables

0
>      Buenas noches quisiera que me puedan ayudar estoy realizando una
>      validación : Bueno la verdad estoy realizando un Logeo que todos los usuarios van tener 3 intentos al ingresar con lenguaje c# con **sql
> server y procedimiento almacenado para el logeo**.  Lo que quiero que se realiza es
> que si el usuario y contraseña es correcta ingrese  , Cuando el
> usuario ingrese su contraseña mala solo le quede 2 intentos y si sigue
> intentando le queda 1 intento pero si en su ultimo intento sigue
> escribiendo mal se bloquea el usuario y ya no ingresa . Para eso en la
> pagina se va mostrar los errores o cuanto intentos le quedan para
> realizar eso lo estoy realizando en sql server como estoy realizando
> un store procedure pero no me logra salir en sql server 
> 
>      1. Que si es verdad el usuario y password que me muestre un select*from usuario y en el campo intentosusuario nuevamente sea 3
>     2. que si es falso el usuario o contraseña el campo de intentossuario que es 3  se reste -1  y asi sucesivamente  y se
> muestre select*from usuario
>      3. Que si en el campo intentosusuario es = 0 que muestre en el campo estadousuario salga como ='bloqueado' select*from usuario
  

no se si me entiendan no soy bueno explicando

in sql server is in my table:

    CREATE TABLE [dbo].[usuario](
        [Idusuario] [int] IDENTITY(1,1) NOT NULL,
        [username] [varchar](200) NULL,
        [llave] [varchar](200) NULL,
        [Nombres] [varchar](350) NULL,
        [Apellidos] [varchar](350) NULL,
        [sexo] [varchar](1) NULL,
        [IDUsuarioPerfil] [varchar](1) NULL,
        [EstadoUsuario] [varchar](20) NULL,
        [IntentosUsuario] [varchar](10) NULL
    ) ON [PRIMARY]


GO
INSERT [dbo].[usuario] ([Idusuario], [username], [llave], [Nombres], [Apellidos], [sexo], [IDUsuarioPerfil], [EstadoUsuario], [IntentosUsuario]) VALUES (1, N'pflores', N'222222', N'Pierro Joshep', N'Flores Espinoza', N'H', N'1', N'ACT', N'3')
GO
INSERT [dbo].[usuario] ([Idusuario], [username], [llave], [Nombres], [Apellidos], [sexo], [IDUsuarioPerfil], [EstadoUsuario], [IntentosUsuario]) VALUES (2, N'Syaringano', N'222222', N'Sandra', N'Yaringaño Vargas', N'M', N'2', N'ACT', N'3')
GO
INSERT [dbo].[usuario] ([Idusuario], [username], [llave], [Nombres], [Apellidos], [sexo], [IDUsuarioPerfil], [EstadoUsuario], [IntentosUsuario]) VALUES (3, N'Dyllescas', N'222222', N'Daniel', N'Yllescas Romani', N'H', N'3', N'ACT', N'3')
GO
SET IDENTITY_INSERT [dbo].[usuario] OFF
GO


Yo creado un procedimiento almacenado que tiene dos variables de entrada que son : 
username y llave 

This is my stored procedure:

ALTER procedure [dbo].[LoginUsuario]
@username varchar(250),
@llave varchar(250)
as
begin

if exists(select * from usuario where username=@username and llave=@llave and EstadoUsuario='ACT' and IntentosUsuario <=(0))
begin
  update colegio..usuario set IntentosUsuario=3 where username=@username
select idusuario, nombres + ' ' + apellidos as 'Nombres',idusuarioperfil,IntentosUsuario ,EstadoUsuario from usuario where username=@username and llave=@llave and EstadoUsuario='ACT'
end

  else if  exists(select * from usuario where username<>@username or llave<>@llave and EstadoUsuario='ACT' and IntentosUsuario<=(0))
begin
declare @num int,@numerorestar int
set @num=(select intentosusuario from usuario where username=@username or llave=@llave and EstadoUsuario='ACT')
set @numerorestar=(@num-1)
update usuario set IntentosUsuario=@numerorestar  where username=@username or llave=@llave and EstadoUsuario='ACT'
update usuario set EstadoUsuario = case  when IntentosUsuario<=0 then 'BLOQUEADO' Else 'ACT' end 
select idusuario=0, nombres + ' ' + apellidos as 'Nombres',idusuarioperfil,IntentosUsuario,EstadoUsuario from usuario where username=@username or llave=@llave

end

end

The problem is when I enter the user or password. Right I get this error:

    
asked by PieroDev 13.05.2017 в 07:44
source

2 answers

2

The problem seems to be this sentence:

set @num=(select intentosusuario from usuario where username=@username or llave=@llave and EstadoUsuario='ACT')

You should return a record but you are recovering more. Execute it individually with the data with which I tell you the error to verify the logic. The problem seems to be the OR.

Notice this sentence:

select intentosusuario from usuario where username='pflores' or llave='222222' and EstadoUsuario='ACT'

The return is

NULL
NULL
NULL

Indeed, if you notice, it returns 3 records when in fact it should return a single record for the user you are looking for. You have two problems, on the one hand the OR is actually AND, and besides you are not considering the NULL of user attempts, I suggest this modification:

select isnull(intentosusuario,0) from usuario where username='pflores' AND llave='222222' and EstadoUsuario='ACT'
    
answered by 13.05.2017 в 16:38
0

Good I solved it but I do not know if it's the right thing to do but it turned out I leave the code: What you realize is first that in an if validate if the user exists or not if in case there exists what will be 1 that his attempts to achieve enter with 3 But if in case there is no user in a variable I am only stored the number of attempts and subtracting 1 from the same user but if in case the number of attempts is less than or equal to 0 that modifies the user state field to blocked and in the end after of the enf I just put the entire table shown according to the user they enter.

ALTER procedure [dbo].[LoginUsuario]
@username varchar(250),
@llave varchar(250)
as
begin

if exists(select*from usuario where username=@username and llave=@llave)
begin
--select*from usuario where username=@username

update usuario set IntentosUsuario=3 where username=@username
end
else if  exists (select *from usuario where username<>@username)
begin

declare @num int,@numerorestar int
set @num=(select intentosusuario from usuario where username=@username  and EstadoUsuario='ACT')
set @numerorestar=(@num-1)
update usuario set IntentosUsuario=@numerorestar  where username=@username  and EstadoUsuario='ACT'
update usuario set EstadoUsuario=(case when (IntentosUsuario<='0') then 'BLO' else 'ACT'end)
end
select*from usuario where username=@username
end
    
answered by 14.05.2017 в 00:36