Subject: Authentication and Role-Based Security
Posted By: swandown Post Date: 10/11/2004 4:07:49 PM
Hi,
I'm using Daniel Cazzulino's excellent book "Beginning C# Web Applications with Visual Studio .NET". In his example in chapter 10 he creates a GenericPrincipal containg the role of a user (which will later show or hide a link to an admin page). Here is a code snippet :

    ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
sql = "SELECT IsAdministrator FROM [User] WHERE UserId='{0}'";
sql = String.Format(sql, id);
cmd = new SqlCommand(sql, con);
con.Open();

object admin = cmd.ExecuteScalar();

// Was it a valid UserID?
if (admin != null)
  {
  GenericPrincipal ppal;
  string[] roles;

    // If IsAdministrator field is true, add both roles
    if (((bool)admin) == true)
        {
        roles = new string[] {"User", "Admin"};
        }
     else
        {
        roles = new string[] {"User"};

........

his role is stored as a boolean and inspected as such. But I have several roles which are probably better stored as int. Has anybody got any advice how I might achieve this? Something like :

int admin = cmd.ExecuteScalar();
.....
if (admin == 1){roles = new string[] {"User", "Approver","Admin"};}
elsif (admin == 2){roles = new string[] {"User", "Approver"};}
else {roles = new string[] {"User"};}

Maybe??

Go to topic 20539

Return to index page 747
Return to index page 746
Return to index page 745
Return to index page 744
Return to index page 743
Return to index page 742
Return to index page 741
Return to index page 740
Return to index page 739
Return to index page 738