tags:

views:

160

answers:

2

Hi,

In my VS solution, I have two projects.One for the Web Interface, other for DataAcess and BusinessLogic. I know I can check if the currently logged-on user is Employee in Web Interface project like this from the code behind:

Dim isEmployee = User.IsInRole("Employee")

The problem is that I have a Class call UserManagement in my the DA and BL project which I want to check the currently logged-on user role also. I can't use Dim isEmployee = User.IsInRole("Employee") because it doesn't have aspx page.

What do I need to do to check the user role in my custom class?

Thank you.

A: 

In your web application, when you initially determine the role(s) for a user, that code should be calling business objects of some sort that make the determination. So the dependency is from your web app to your business layer (i.e. your web app requires your business layer), not the other way around.

TheObjectGuy
I know... but that what's I'm asking for. What do I have to do to check if a user is in a role in the custom class of business layer? I know only how to check in the code behind.
Angkor Wat
Bayonian: How do you currenly implement `IsInRole()` in your web app? Using a standard or a custom provider?
Jørn Schou-Rode
Using Standard provider.
Angkor Wat
A: 

You need to reference System.Web in your business project. Then do the following:

    Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
    Dim isRole As Boolean = context.User.IsInRole("Admin")

or c#

System.Web.HttpContext context = System.Web.HttpContext.Current;
bool isRole = context.User.IsInRole("Admin");
Rick Ratayczak
Thank you so much. How do I check if a user in a particular role by userId (or username) because I have a userId as foreign key in a table? Actually, my function take a userId as param to check for a role.
Angkor Wat
See the code below in the next comment, and don't forget to mark this as an answer if it was helpful!
Rick Ratayczak
Dim isInRole As Boolean = System.Web.Security.Roles.IsUserInRole("Bayonian", "Admin")
Rick Ratayczak
I never think of adding System.Web reference in the business project. Thank you. This made my day.
Angkor Wat