I'm having trouble figuring out how to assign roles to users. I've got the log in mechanism working, but need to figure out how to use roles defined to give users certain access.
I have a database with these tables:
Tables ------ UserTbl RolesTbl UserInRoleTbl ------- ---------- ------------- UserID (PK) RoleId (PK) ID (PK) Name RoleName UserId UserName Description RoleId Password Email
With this small database I'm testing the ability to assign roles to the user.
I'm using LINQtoSQL
to act as the Data Access Layer. I've created the login mechanism to login to the software, and then I get stuck on what to do next.
For Example:
username = admin,password= admin, RoleId = 1 ;Rolename =Administrator;
And then I use the following code after the login to get the role: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;
}
}
I implement the code role but there is no connection with database; instead I want to store the role from the database,create a method in C# and write down a code that after user's login to get access to the application.
To explain further: I gave Rolename = Administrator
so if the user is admin he'll get this role, but I'm unsure how to retreive this information from the database and tie it to the user. Can anyone assist me?
Related (or possible duplicate) questions by Same User:
http://stackoverflow.com/questions/550667/username-and-role
http://stackoverflow.com/questions/544517/forms-authenticate
http://stackoverflow.com/questions/557791/how-create-role-to-put-in-the-database