How do I get the role I am in?

How do I get the role I am in?

Old forum URL: forums.lhotka.net/forums/t/4381.aspx


CyclingFoodmanPA posted on Wednesday, February 20, 2008

I am creating a fairly large application utilizing CSLA 3.0 and have created a number of reports utilizing SQL Server 2005 Reporting Services.  The user will be able to select the reports appropriate to his role.  I have a stored procedure usp_csla_SelectRoleSpecificReports that accepts a parameter and that is the role as I have a table called tblReports, a table called tblRoles and a table tblReportRole.  My usp queries those tables and only returns the reports which a particular role can see, i.e. (Accounting, Operations, Marketing, etc.)  What I am trying to figure out is how do I get the role that the current user is logged in as.  All the other methods Csla.ApplicationContext.User.IsInRole() checks to see if the user is in a particular role, but, how do I get the role to be able to pass to my usp?

Thanks in advance for your help.

Keith

 

Curelom replied on Wednesday, February 20, 2008

If you are using a custom identity object, you could add a property to retrieve the list of roles for that user.  To use them you would then cast the identity as the custom identity object, then call the property/method.

JonnyBee replied on Thursday, February 21, 2008

The following code shows how to retrieve the Group (role) names from a WindowsIdentity:

       /// <summary>
        /// Gets the windows roles for current user .
        /// </summary>
        /// <returns></returns>
        private static string[] GetWindowsRoles() {
            List<string> rolelist = new List<string>();
           
            WindowsIdentity id = WindowsIdentity.GetCurrent();
          
            foreach (IdentityReference ir in id.Groups) {
                NTAccount name = (NTAccount)ir.Translate(typeof(NTAccount));
                rolelist.Add(name.ToString());
            }

            // return array of roles
            return rolelist.ToArray();
        }

Beware tho' - this may be a litte timeconsuming to execute as it iterates the users roles (SID's) and tranlates to clear text names. Text name is on format "domain\groupname".

Is this what you were looking for?

/jonny


Copyright (c) Marimer LLC