为用户分配角色

时间:2009-02-22 17:53:21

标签: c# .net wpf database linq-to-sql

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

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

                     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 个答案:

答案 0 :(得分:3)

抱歉,我觉得你的问题有点令人困惑。

您似乎希望拥有两个表,User和Role,其中包含多对多连接表UserRole。用户和角色必须都有主键,这两个键都出现在连接表中。

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”从表名中删除。在我看来,它们是多余的。

答案 1 :(得分:2)

如果数据库中有“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();

这有帮助吗?

马克