• 会员系统中需要验证用户的邮箱是否真实存在


    在开发网站时,我们需要对用户注册的邮箱进行核对与验证,用户填写的邮箱是否有效邮箱。

    好吧,我们先从数据库入手,修改用户表让用户有填写email的字段,添加了2个字段:

    ALTER TABLE [dbo].[Users]
    ADD [Email] VARCHAR(100) NULL, 
    [IsVerify] BIT NOT NULL DEFAULT(0)
    
    
     SELECT * FROM [dbo].[Users]
    Source Code


    由于你需要做2个功能,一个是要求用户验证邮箱有效性,也有可以以邮箱来让用户修改用户密码。因此需要创建一个表来存储这2个类型的数据:


    CREATE TABLE [dbo].[RequestActionType]
    (
        [Type] NVARCHAR(2) NOT NULL PRIMARY KEY,
        [Description] NVARCHAR(30) NULL
    )
    GO
    INSERT INTO [dbo].[RequestActionType] ([Type],[Description])
    VALUES ('V',N'验证邮箱是否有效。'),
    ('C',N'用户修改密码')
    Source Code

    接下来,你还需要创建另外一张表,是记用户请求的事件,记录用户的一些信息,如帐号,邮箱,链接有时效性等:

    CREATE TABLE [dbo].[UserRequestAction](
        [Type] NVARCHAR(2) NOT NULL FOREIGN KEY REFERENCES [dbo].[RequestActionType] ([Type]),
        [Token] [uniqueidentifier] NOT NULL DEFAULT(NEWID()),
        [Account] [nvarchar](30) NOT NULL,
        [Email] [nvarchar](150) NOT NULL,
        [Expire] [datetime] NOT NULL DEFAULT (DATEADD(day,(1),CURRENT_TIMESTAMP)),
    )
    
    GO
    Source Code


    当用户更改邮箱成功时,需要同进对[IsVerify] 更改为false。因此你需要对最开始的表写一个触发器:


    CREATE TRIGGER [dbo].[tri_Users_Update] 
    ON [dbo].[Users]
    FOR UPDATE
    AS
    DECLARE @U_nbr NVARCHAR(20),@IsVerify BIT
    DECLARE @old_email VARCHAR(100),@new_email VARCHAR(100)
    
    SELECT @new_email = [Email] FROM INSERTED
    SELECT @U_nbr = [U_nbr],@old_email = [Email],@IsVerify = [IsVerify] FROM DELETED
    
    IF @IsVerify = 1 AND (lEN(ISNULL(@new_email,'')) = 0 OR  @new_email <> @old_email)
        UPDATE [dbo].[Users] SET [IsVerify] = 0 WHERE [U_nbr] = @U_nbr
    GO
    Source Code

     
    当用户发出验证邮箱或是更改密码时,让程序执行下面的存储过程:

    CREATE PROCEDURE [dbo].[usp_UserRequestAction_Request]
    (
        @Type NVARCHAR(2),
        @U_nbr NVARCHAR(20)
    )
    AS
    IF NOT EXISTS(SELECT TOP 1 1 FROM [dbo].[Users] WHERE [U_nbr] = @U_nbr)
    BEGIN
        RAISERROR(N'帐号错误或不存存在,请联系系统管理员。',16,1)
        RETURN
    END
    
    DECLARE @Email NVARCHAR(100)
    SELECT @Email = [Email] FROM [dbo].[Users] WHERE [U_nbr] = @U_nbr
    
    
    IF EXISTS(SELECT TOP 1 1 FROM [dbo].[UserRequestAction] WHERE [Type] = @Type AND [Account] = @U_nbr AND [Email] = @Email)
        UPDATE [dbo].[UserRequestAction] SET [Token] = NEWID(),[Expire] = DATEADD(day,(1),CURRENT_TIMESTAMP) WHERE [Type] = @Type AND [Account] = @U_nbr AND [Email] = @Email
    ELSE
        INSERT INTO [dbo].[UserRequestAction] ([Type],[Account],[Email]) VALUES (@Type,@U_nbr,@Email)
    GO
    Source Code


    用户验证邮箱有效性,是在登录之后进行的,因此只需要点击“验证”铵钮即可,系统即发送验证的邮件至用户的邮箱中。
    另外,当用户忘记密码时,是在没有登录系统之下进行的,因此需要输入用户的帐号才能进行下一步。
    均是使用这个存储过程[dbo].[usp_UserRequestAction_Request]。

     
    接下来的流程是,用户会打开他的邮箱,查阅刚刚系统发送的邮件。邮件内容就是看实际需求了,如提示用户,是不是自己本人操作,安全性等,这些都不是怎样重要,重要的是那一条链接。
    指示用户点击链接。这个链接会导上到网站一个页面。当到这个页面时,系统会在这页面进行一些程序处理,检查链接有效性,时间是否过期,如果一切没有问题,会进更新IsVerify字段为ture.

    如果是用户忘记密码的话,在用户点击链接,系统也会检有效性,没有期,面会出现更改密码的form,让用户进行更改全新的密码。

    Ok,还差2个存储过程,第一个是更新IsVerify字段值:

    CREATE PROCEDURE [dbo].[usp_Users_UpdateIsVerifyField]
    (
        @token NVARCHAR(36)
    )
    AS
    IF EXISTS(SELECT TOP 1 1 FROM [dbo].[UserRequestAction] WHERE [Token] = @token AND [Expire] >= CURRENT_TIMESTAMP)
    BEGIN
        DECLARE @Account NVARCHAR(30)
        SELECT @Account = [Account] FROM [dbo].[UserRequestAction] WHERE [Token] = @token
            
        UPDATE [dbo].[Users] SET [IsVerify] = 1 WHERE [U_nbr] = @Account
    
        UPDATE [dbo].[UserRequestAction] SET [Expire] = DATEADD(DAY,-1,CURRENT_TIMESTAMP) WHERE [Token] = @token
    END
    GO
    Source Code


    另一个是ResetPassword的,重设密码:

    CREATE PROCEDURE [dbo].[usp_Users_ResetPassword]
    (
        @token NVARCHAR(36),
        @Password NVARCHAR(100)
    )
    AS
    IF EXISTS(SELECT TOP 1 1 FROM [dbo].[UserRequestAction] WHERE [Token] = @token AND [Expire] >= CURRENT_TIMESTAMP)
    BEGIN
        DECLARE @Account NVARCHAR(30)
        SELECT @Account = [Account] FROM [dbo].[UserRequestAction] WHERE [Token] = @token
    
        DECLARE @pwd VARBINARY(MAX) = ENCRYPTBYPASSPHRASE('insus#sec!%y',@Password)
        UPDATE [dbo].[Users] SET [Pwd] = @pwd WHERE [U_nbr] = @Account 
    
        UPDATE [dbo].[UserRequestAction] SET [Expire] = DATEADD(DAY,-1,CURRENT_TIMESTAMP) WHERE [Token] = @token
    END
    ELSE
    BEGIN
        RAISERROR(N'无法更改密码,请联系客服或网络管理员。',16,1)
        RETURN
    END
    Source Code

     数据库方面开发就这样子,程序方面看你自己发挥了。

  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/insus/p/5996385.html
Copyright © 2020-2023  润新知