添加自定义角色 - VB 2008 Winforms

时间:2009-09-02 23:53:08

标签: vb.net winforms authentication user-roles

我有一个基于winforms(VB 2008)的应用程序,我正在开发,我想使用自定义角色进行用户访问。

应用程序布局: 我有一个主窗体,在某些操作发生时打开登录表单。 Login表单实习生使用我创建的身份验证类来验证用户身份并设置访问权限。在我的应用程序设置页面上,我将身份验证模式设置为应用程序定义,因为我无法在将部署此身份验证的环境中使用Windows身份验证。

应用程序使用MS SQL 2005 db,我在身份验证过程中使用的3个表是User_Account,User_Roles和User_Access表。 User_Account中的帐户与User_Roles表中的角色的组合是User_Access表的基础。使用User_Access表是我如何分配对应用程序中各种功能的访问

身份验证方法: 要对用户进行身份验证,我使用的是“My.User.CurrentPrincipal”(下面的代码)方法。 My.User对象运行良好,允许在引用当前经过身份验证的用户时在整个应用程序中使用“My.User.Name”属性。

访问方式 为了设置当前用户的访问级别,我在Authentication类中使用了一个函数,并将My.User.Name作为变量传递。该函数在For循环内使用数据集表适配器和Select Case语句为用户分配所有访问级别(下面的功能代码)。

我的问题: 这种为用户分配访问权限的方法确实有效,但它在整个应用程序中并不像My.User对象那样持久。我想找到一种方法通过My.User对象使用其.IsInRole属性创建自定义角色。我想使用我的User_Roles表动态创建这些角色。 这将允许使用My.User.IsInRole(“MyRole”)语法在我的应用程序中使用自定义角色...类似于我当前能够使用My.User.Name的方式。不幸的是,我目前可以验证的唯一角色是内置的Windows类型帐户(Adminisrator等等)。

我找到了很多与ASP.Net相关的信息和示例,以及设置Winforms Windows身份验证,但到目前为止还没有与我的问题直接相关。 我认为有办法实现这一目标......但我找不到它。任何帮助将不胜感激!!

感谢您的帮助!


'用户身份验证示例:

If Authenticate.CheckPassword(tbxUserName.Text, strPassword) Then
            My.User.CurrentPrincipal = New GenericPrincipal(New GenericIdentity(tbxUserName.Text), Nothing)

'访问权限分配示例:

 Public Shared Function GetUser(ByVal strUsername As String) As Authenticate
        Using UserAdapter As New dbUserTableAdapters.User_AccountsTableAdapter()
            Dim UserTable As dbUser.User_AccountsDataTable = UserAdapter.GetByUser(strUsername)


            Dim tempUser As New Authenticate() _
                With {.ID = UserTable(0).id, _
                    .Username = UserTable(0).User_Name, _
                    .Password = UserTable(0).id}

            Using AccessAdapter As New dbUserTableAdapters.User_AccessTableAdapter()
                Dim AccessTable As dbUser.User_AccessDataTable = AccessAdapter.GetByUser(tempUser.ID)

                For c As Integer = 0 To AccessTable.Rows.Count - 1

                    Select Case AccessTable(c).Role_Id
                        Case RoleType.SysAdmin
                            tempUser.AllowSysAdmin = True

                        Case RoleType.Maintenance
                            tempUser.AllowMaintenance = True

                        Case RoleType.ReportAll
                            tempUser.AllowRptAll = True

                        Case RoleType.ReportException
                            tempUser.AllowRptExceptions = True

                        Case RoleType.EventManagment
                            tempUser.AllowEventStart = True
                        Case Else

                    End Select

                Next

                Return tempUser

            End Using
        End Using
    End Function

1 个答案:

答案 0 :(得分:0)

我认为您需要实现一个访问SQL表的自定义IPrincipal对象。试试this页。

编辑:

首先,看看IIdentityIPrincipal的定义。您会注意到IIdentity没有定义“角色”属性。他们选择在实施IIdentity(SampleIIdentity)时实现一个名为Role的附加属性,然后他们从IPrincipal的实现中使用它。我建议您实现自己的Role属性(查询现有表)并返回您自己定义的Role类型的一个(或数组)。然后在IPrincipal的实现中,您可以编写IsInRole代码来查询新的Role属性。希望这对我的回答更加有意义。

相关问题