代码之家  ›  专栏  ›  技术社区  ›  JayJay

为用户分配角色

  •  2
  • JayJay  · 技术社区  · 16 年前

    我很难弄清楚如何为用户分配角色。我已经启动了登录机制,但是需要弄清楚如何使用定义的角色来为用户提供一定的访问权限。

    我有一个包含这些表的数据库:

                         Tables
                         ------
       UserTbl          RolesTbl           UserInRoleTbl
       -------         ----------          -------------
     UserID  (PK)      RoleId  (PK)        ID       (PK)
     Name              RoleName            UserId
     UserName          Description         RoleId
     Password
     Email
    
    

    有了这个小数据库,我正在测试为用户分配角色的能力。

    我在用 LINQtoSQL 作为数据访问层。我已经创建了登录机制来登录软件,然后我就陷入了下一步该做什么的困境。

    例如:

    username = admin,password= admin, RoleId = 1 ;Rolename =Administrator;

    然后在登录后使用以下代码获取角色:n

    public partial class Window3 : Window
    {  
    
        public Window3()
        {
            InitializeComponent();
    
            GenericIdentity My2 = new GenericIdentity("admin");
    
            string[] roles1 = { "Administrator" };
    
            GenericPrincipal principal1 = new GenericPrincipal(My2, roles1);
            Thread.CurrentPrincipal = principal1;
         }
    
    private void LoadWindow(object sender, RoutedEventArgs e)
        {   if (Thread.CurrentPrincipal.IsInRole("Administrator"))
            {
                 exercise.Visibility = Visibility;
                  tot.IsEnabled = false;
            }
    

    }

    我实现了代码角色,但是没有与数据库的连接;相反,我希望从数据库中存储该角色,在C中创建一个方法,并在用户登录后写下一个代码,以便访问应用程序。

    进一步解释:我给了 Rolename = Administrator 所以,如果用户是管理员,他会得到这个角色,但我不确定如何从数据库中检索这个信息并将其绑定到用户。有人能帮我吗?

    同一用户提出的相关(或可能重复)问题:

    Username and role
    Forms Authenticate
    How create role to put in the database?

    2 回复  |  直到 16 年前
        1
  •  2
  •   marc_s    16 年前

    如果数据库中有“usertbl”和“rolestbl”,那么在linq-to-sql模型中还应该有两个名为“usertbl”和“rolestbl”的类。

    要在数据库中存储角色,请实例化一个“rolestbl”对象,设置其属性,然后将其添加到Linq数据上下文。与“usertbl”对象相同。

    编辑:下面是请求的代码示例-假设您已经设置了数据库、LINQ to SQL模型并使用了所有默认名称:

    // create the LINQ-to-SQL Data context
    UsersAndRolesDataContext dc = new UsersAndRolesDataContext();
    
    // create new instace of "UserTbl" object
    UserTbl newUser = new UserTbl();
    newUser.UserID = "newuser";
    newUser.Name = "Some NewUser";
    newUser.EMail = "newuser@somewhere.org";
    newUser.Password = "TopSecret";
    
    // add new user to the table of users in the data context
    dc.UserTbls.InsertOnSubmit(newUser);
    
    // create new instance of a role
    RolesTbl newRole = new RolesTbl();
    newRole.RoleId = "ADMIN";
    newRole.RoleName = "Administrators";
    newRole.Description = "User with administrative rights";
    
    // add new role into LINQ-to-SQL Data context
    dc.RolesTbls.InsertOnSubmit(newRole);
    
    // write out all changes back to database
    dc.SubmitChanges();
    

    这有帮助吗?

    马克

        2
  •  3
  •   duffymo    16 年前

    对不起,我觉得你的问题有点困惑。

    您似乎想要有两个表,用户和角色,中间有一个多对多的连接表,用户角色。用户和角色都必须具有主键,这两个主键都出现在联接表中。

    create table user
    (
        user_id int not null, 
        -- other fields here, like username, password, etc.
        primary key(user_id)
    );
    
    create table role
    (
        role_id int not null,
        -- other fields here, like role name, etc.
        primary key(role_id)
    );
    
    create table user_role
    (
        user_id int not null,
        role_id int not null,
        primary key(user_id, role_id),
        foreign key(user_id) references(user),
        foreign key(role_id) references(role)
    );
    

    当您查询一个用户以查看他们是否被授权时,您将加入多对多表以同时恢复所有潜在角色。如果您提供的凭证包含一个角色,您的授权代码应该检查以确保它是当时潜在角色集的成员。

    一个小建议:把“tbl”从表名中去掉。在我看来,它们是多余的。