在存储过程中验证用户的最简单方法?

时间:2012-01-17 13:02:20

标签: sql sql-server-2005 tsql

我需要一个存储过程,可以通过发送loginpassword来检查是否在登录尝试时是否是有效用户,以查看它们是否在数据库中匹配。有一种简单的方法可以做到这一点吗?

5 个答案:

答案 0 :(得分:6)

如果没有更多信息,我现在能提供的最好的信息是:

CREATE STORED PROCEDURE CheckPassword
    @username VARCHAR(20),
    @password varchar(20)
AS
BEGIN

SET NOCOUNT ON

IF EXISTS(SELECT * FROM usertable WHERE username = @username AND password = @password)
    SELECT 'true' AS UserExists
ELSE
    SELECT 'false' AS UserExists

END

根据您的回复修改查询 - 如果您愿意,这将返回字符串'true'或'false',您可以分别用位值1和0替换它们。

答案 1 :(得分:3)

这可能会有所帮助:

CREATE PROCEDURE CheckPassword
    @username VARCHAR(20),
    @password varchar(20)
AS
BEGIN

SET NOCOUNT ON

SELECT CASE WHEN EXISTS(SELECT NULL FROM usertable WHERE userName=@username AND password=@password)
    THEN CAST(1 AS BIT)
    ELSE CAST(0 AS BIT)
END

END

答案 2 :(得分:0)

Create proc usp_ValidateStoreKeeperLogin 
@SalesmanCode VARCHAR(50)
,@LogisticUserCode VARCHAR(50)
,@LogisticUserPassword VARCHAR(50)

AS 
BEGIN

if EXISTS(select 1 from tblUser where Code=@LogisticUserCode And [password]=@LogisticUserPassword  )
SELECT '1234' SalesmanCode,'12345' LogisticUserCode,'12346' DistributorCode,1 as ReturnValue,'Success' AS Msg from tblUser

select 'INVALID USER CODE' AS Msg ,-1 as ReturnValue


END

答案 3 :(得分:0)

go
CREATE PROC usp_ValidateUser
( @userName VARCHAR(50),
    @password VARCHAR(50)
)
AS
begin
declare @credentials TABLE(
    username varchar(50),
    userPassword varchar(50)
)

SELECT null,
    CASE WHEN NombreUsuario = 'korn' and PasswordUsuario = HASHBYTES('sha1', '1234') THEN  cast(1 as bit)
        ELSE   cast(0 as bit) end as TieneAcceso

FROM Usuarios;
end

答案 4 :(得分:-1)

Create procedure validate_data

@username varchar(20),
@password varchar(20)

As 
Begin 
  If exists (select * from employee where username=@username and password=@password)
  Raiserror('Exists'16,1)
Else 
  Raiserror('Not Exists'16,1)
End

在这里我以employee作为表,用户名和密码包含employee表。