身份验证示例

下面介绍如何在用户登录的时候,使用UKey进行身份验证。

使用系统登录时的身份验证

如果使用Foxtable自带的用户管理和登录窗口,可以非常简单地加上UKey身份验证功能。
只需在用户属性窗口的Tag输入框,输入分配给该用户的UKey的UID即可,注意UID前面必须加上两个“*”号:

那么此用户登录的时候,必须插上分配给他的UKey,否则即使输入正确的用户名和密码,也会提示:

自定义用户管理时身份验证


如果使用自定义用户管理,加上UKey身份验证功能也是比较方便的。
关于自定义用户管理,可以先参考:自定义用户与权限管理,现在要在这个基础上加上UKey身份验证功能。
 

1、在原来的“Users”表增加一名为UID的字符列,现在此表合计有Name、Group、Password、UID四列,分别用于保存用户名、用户分组、密码和UID。

2、修改“更改用户”窗口,加上输入UID的输入框,并将此输入框的名称改为UID:

2、将“更改用户”窗口的AfterLoad事件代码改为(加粗的一行为新增加的代码):

Dim UserName As String
Dim
UserGroup As String
Dim
PassWord As String
Dim
cmd As New SQLCommand
Dim
dt As DataTable
If
Forms("用户管理").Opened Then '正常打开
   UserName = Forms(
"用户管理").Controls("ListBox1").SelectedItem
Else

   UserName = _UserName
'_UserName为全局变量,用于保存当前用户名
   e.Form.Controls(
"UserName").Enabled = False
   e.Form.Controls(
"UserGroup").Enabled = False
End
If
e.Form.Text = e.Form.Text &
"-" & UserName
cmd.ConnectionName =
"User"
cmd.CommandText =
"Select * From {Users} Where [Name] = '" & UserName & "'"
dt = cmd.ExecuteReader

If
dt.DataRows.Count = 1 Then
    e.Form.Controls(
"UserName").Value = UserName
    e.Form.Controls(
"UserGroup").Value = dt.DataRows(0)("Group")
    e.Form.Controls(
"PassWord").Value = dt.DataRows(0)("PassWord")
    e.Form.Controls(
"UID").Value = dt.DataRows(0)("UID")
Else

    MessageBox.Show(
"用户不存在或者已经被删除!","提示",MessageBoxButtons.OK)
    e.Form.Close()

End
If

3、将“更改用户”窗口的“确定”按钮的代码改为(加粗的两处为新增加或有改动的代码):

Dim UserName As String = e.Form.Controls("UserName").Value
Dim
UserGroup As String  = e.Form.Controls("UserGroup").Value
Dim
PassWord As String = e.Form.Controls("PassWord").Value
Dim
UID As String = e.Form.Controls("UID").Value
Dim
OldUserName As String
Dim
cmd As New SQLCommand
Dim
Parts() As String = e.Form.Text.Split("-"c)
OldUserName =Parts(Parts.Length -1)
cmd.ConnectionName = "User"
If
UserName = "" OrElse UserGroup = "" Then
    Messagebox.show(
"
请输入用户名和用户分组!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
   
Return

End
If
cmd
.CommandText = "Update {Users} Set [Name] = '" & UserName & "',[Group] = '" & UserGroup
cmd
.CommandText = cmd.CommandText & "', [Password] = '" & Password & "', [UID] = '" & UID & "' Where [Name]= '" & OldUserName & "'"
If
cmd.ExecuteNonQuery = 1 Then '返回1表示更改成功
    If Forms(
"
用户管理").Opened Then
        With Forms(
"
用户管理").Controls("ListBox1")
            .Items(.SelectedIndex) = UserName
        End With
    End If
    e.Form.
Close

Else

    Messagebox.show("更改用户失败!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

End
If

4、同样在“增加用户”窗口,加上UID输入框,并将此输入框的名称改为UID:

5、然后将“增加用户”窗口的“确定”按钮代码改为(加粗处为新增加或有变动的代码):

Dim UserName As String = e.Form.Controls("UserName").Value
Dim
UserGroup As String  = e.Form.Controls("UserGroup").Value
Dim
PassWord As String = e.Form.Controls("PassWord").Value
Dim
UID As String = e.Form.Controls("UID").Value
Dim
cmd As New SQLCommand
cmd
.ConnectionName = "User"
If
UserName = "" OrElse UserGroup = "" Then
    Messagebox.show("
请输入用户名和用户分组!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return

End
If
cmd
.CommandText = "Select Count(*) From {Users} Where Name = '" & UserName & "'"
If
cmd.ExecuteScalar > 0 Then
    Messagebox.show("
增加用户失败,已经能存在同名用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return

End
If
cmd
.CommandText = "Insert Into {Users} ([Name],[Group],[Password],[UID]) Values ('"
cmd
.CommandText = cmd.CommandText & UserName & "','" & UserGroup & "','" & Password & "','" & UID & "')"

If
cmd.ExecuteNonQuery = 1 Then '返回1表示增加成功
    If
Forms("用户管理").Opened Then
        With
Forms("用户管理").Controls("ListBox1")
            .
Items.Add(UserName)
            .
SelectedIndex = .Items.Count -1
        End With
    End If
   
e.Form.Close()
End If

6、将“用户登录”窗口的确定按钮代码改为(加粗部分为新增加的代码):

Dim UserName As String = e.Form.Controls("UserName").Value
Dim
cmd As New SQLCommand
Dim
dt As DataTable
Dim
dr As DataRow
cmd
.ConnectionName = "User"
If
UserName = ""  Then
    Messagebox.show("
请选择用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return

End
If
cmd
.CommandText = "Select * From {Users} Where [Name] = '" & UserName & "'"
dt
= cmd.ExecuteReader
dr
= dt.DataRows(0)
Dim
UID As String = dr("UID")
If
UID > "" Then
    If UKey.Start = False OrElse UID <> UKey.UID  Then
        MessageBox.show("
请插上登录用户的UKey!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
        Return
    End If

End If
If
e.Form.Controls("PassWord").Value = dr("Password") Then
   
_UserName = UserName
   
_UserGroup = dr("Group")
   
e.Form.Close
Else
   
Messagebox.show("密码错误!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
End If


本页地址:http://www.foxtable.com/webhelp/topics/2664.htm