tags:

views:

33

answers:

2

hi,

Can I use IsInRole with customized objects?? Like I want to do some operations only for Employee while other only for Managers.

How can I achieve this?

+1  A: 

Yes you can:

var isMgr = User.IsInRole("Managers");
if(isMgr){
   DoManagerialWork();
}else{
   AccessDenied();
}

but you have to wireup asp.net membership etc. in your (asp.net) application.

TheVillageIdiot
for if(User.IsInRole("Employees") to return true do I need to add any additional code?
Ram
If currently logged user is assigned the role "Employees" and `User.IsInRole` returns true. Now for the code that you want only users in "Employee" role to run, you must put it in the if block `if(User.IsInRole("Employee"){ //your code}`
TheVillageIdiot
+1  A: 

Make use of the built in features:

This is the way you can to id in a Windows Application

using System.Security.Principal;
... 
var currentUser = WindowsIdentity.GetCurrent();
var winPrincipal = new WindowsPrincipal(currentUser);

if(winPrincipal.IsInRole("Employees")) {
  // TODO: BANANAS
} else if (windPrincipal.IsInRole("Managers")) {
  // TODO: APPLES
}
...

This is the way you can do it in ASP.NET:

if(User.IsInRole("Employees")) {
  // TODO: BANANAS
} else if (User.IsInRole("Managers")) {
  // TODO: APPLES
}

BANANS and APPLES would be what your users get ;-)

Shaharyar
for if(User.IsInRole("Employees") to return true do I need to add any additional code?
Ram
Well if you're on a Windows Application, you'll have to write additional code. I've updated my answer.
Shaharyar