- 登陆验证(在激活前提下,才可登陆)
- 用户注册
- 账号激活
- 重发激活邮件
- 密码找回
为实验这模块,我们可以提出这样的解决方案:
A.数据库设计
表Tao_Users存放用户信息,
主要字段:
UserID 用户唯一标识
Email 用户Email
NickName 用户昵称
Password 用户密码
State 状态(0为未激活,1为已激活)
RegTime 注册时间(可以用来删除数据库中长时间未激活的账号)
表Tao_Valid存放验证信息
主要字段:
GUID 全局唯一标识符
Type 验证类型(1为账号激活时用,2为密码找回时用)
UserID 用户唯一标识
AddTime 添加时间(可以定期删除长时间未用数据)
B.个功能实现(这里主要写有关存储过程)
用户注册的存储过程:
--用户注册
CREATE PROCEDURE Tao_UserReg
(
@Email NVarChar(100),
@Password NVarChar(50),
@NickName NVarChar(50),
@GUID NVarChar(50)=null OutPut --返回激活码,发往用户邮箱
)
AS
if not exists(select * from Tao_Users where Email=@Email or NickName=@NickName)
begin
Insert Into Tao_Users(Email,NickName,Password,RegTime)
Values(@Email, @NickName, @Password,GetDate())
Select @GUID = NewID();--由SQL生存一个GUID
--插入验证信息表
Insert Into Tao_Valid (GUID, UserID,Type,AddTime)
Values (@GUID, @@Identity,1,GetDate())
end
GO
CREATE PROCEDURE Tao_UserReg
(
@Email NVarChar(100),
@Password NVarChar(50),
@NickName NVarChar(50),
@GUID NVarChar(50)=null OutPut --返回激活码,发往用户邮箱
)
AS
if not exists(select * from Tao_Users where Email=@Email or NickName=@NickName)
begin
Insert Into Tao_Users(Email,NickName,Password,RegTime)
Values(@Email, @NickName, @Password,GetDate())
Select @GUID = NewID();--由SQL生存一个GUID
--插入验证信息表
Insert Into Tao_Valid (GUID, UserID,Type,AddTime)
Values (@GUID, @@Identity,1,GetDate())
end
GO
数据库返回一个GUID后,我们再往用户邮箱中发送验证Email,用户收到激活邮件后,进行账号激活
--激活账号,返回用户信息(自动登陆系统)
CREATE PROCEDURE Tao_UserActive
(
@GUID NVarChar(50)
)
AS
declare @UserID int --存放用户ID
Select @UserID=UserID From Tao_Valid Where GUID=@GUID And Type=1
--删除验证表中记录
Delete From Tao_Valid Where GUID=@GUID And Type=1
--更新用户状态
Update Tao_Users Set State=1 Where UserID= @UserID
--返回用户信息,自动登陆
Select * From Tao_Users Where UserID= @UserID And State=1
GO
CREATE PROCEDURE Tao_UserActive
(
@GUID NVarChar(50)
)
AS
declare @UserID int --存放用户ID
Select @UserID=UserID From Tao_Valid Where GUID=@GUID And Type=1
--删除验证表中记录
Delete From Tao_Valid Where GUID=@GUID And Type=1
--更新用户状态
Update Tao_Users Set State=1 Where UserID= @UserID
--返回用户信息,自动登陆
Select * From Tao_Users Where UserID= @UserID And State=1
GO
--重发激活码
CREATE PROCEDURE Tao_UserAgainActive
(
@Email NVarChar(100),
@GUID NVarChar(50) Output
)
AS
declare @UserID int
--从用户表中根据Email,查出UserID
Select @UserID=UserID
From Tao_Users
Where State=0 And Email=@Email
--如果找到有此未激活用户
if NOT(@UserID=NULL)
Begin
--删除原有激活信息
Delete From Tao_Valid Where UserID=@UserID
Select @GUID=NewID() --重新生存ID
Insert Into Tao_Valid
(GUID,UserID,Type,AddTime)
Values (@GUID,@UserID,1,GetDate())
End
找回密码部分(返回GUID后,发往用户邮箱)CREATE PROCEDURE Tao_UserAgainActive
(
@Email NVarChar(100),
@GUID NVarChar(50) Output
)
AS
declare @UserID int
--从用户表中根据Email,查出UserID
Select @UserID=UserID
From Tao_Users
Where State=0 And Email=@Email
--如果找到有此未激活用户
if NOT(@UserID=NULL)
Begin
--删除原有激活信息
Delete From Tao_Valid Where UserID=@UserID
Select @GUID=NewID() --重新生存ID
Insert Into Tao_Valid
(GUID,UserID,Type,AddTime)
Values (@GUID,@UserID,1,GetDate())
End
--发送找回密码验证码
Create Procedure Tao_UserCPwdCode
(
@Email NVarChar(100),
@GUID NVarChar(50)OutPut
)
AS
Declare @UserID int
--从用户表中查出UserID
Select @UserID=UserID
From Tao_Users
Where State=1 And Email=@Email
--如果此用户
If Not(@UserID=Null)
Begin
--删除原有更改密码验证码
Delete From Tao_Valid Where UserID=@UserID And Type=2
Select @GUID=NewID()
--插入新验证码
Insert Into Tao_Valid
(GUID,UserID,Type,AddTime)
Values(@GUID,@UserID,2,GetDate())
End
Create Procedure Tao_UserCPwdCode
(
@Email NVarChar(100),
@GUID NVarChar(50)OutPut
)
AS
Declare @UserID int
--从用户表中查出UserID
Select @UserID=UserID
From Tao_Users
Where State=1 And Email=@Email
--如果此用户
If Not(@UserID=Null)
Begin
--删除原有更改密码验证码
Delete From Tao_Valid Where UserID=@UserID And Type=2
Select @GUID=NewID()
--插入新验证码
Insert Into Tao_Valid
(GUID,UserID,Type,AddTime)
Values(@GUID,@UserID,2,GetDate())
End
密码重设部分,根据用户邮箱中的更改密码链接GUID,获取用户昵称,进入更改密码页面,
--密码找回
CREATE PROCEDURE Tao_UserCPwd
(
@GUID NVarChar(50)=null,
@NewPwd NVarChar(50)=null
)
AS
if(@NewPwd=null)
--返回用户昵称,UI中进入重设密码页面
begin
Select U.NickName From Tao_Users AS U,Tao_Valid AS V
Where V.UserID=U.UserID AND V.GUID=@GUID
end
--更改用户密码
Else Begin
declare @UserID int
Select @UserID=UserID From Tao_Valid Where GUID=@GUID And Type=2
Delete From Tao_Valid WHere GUID=@GUID And Type=1
Update Tao_Users Set State=1,Password=@NewPwd Where UserID= @UserID
Select * From Tao_Users Where UserID= @UserID
End
GO
CREATE PROCEDURE Tao_UserCPwd
(
@GUID NVarChar(50)=null,
@NewPwd NVarChar(50)=null
)
AS
if(@NewPwd=null)
--返回用户昵称,UI中进入重设密码页面
begin
Select U.NickName From Tao_Users AS U,Tao_Valid AS V
Where V.UserID=U.UserID AND V.GUID=@GUID
end
--更改用户密码
Else Begin
declare @UserID int
Select @UserID=UserID From Tao_Valid Where GUID=@GUID And Type=2
Delete From Tao_Valid WHere GUID=@GUID And Type=1
Update Tao_Users Set State=1,Password=@NewPwd Where UserID= @UserID
Select * From Tao_Users Where UserID= @UserID
End
GO
这样就完成了整个账户管理部分模块.