1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
public class RoleProviderPerso:RoleProvider
{
private IsecService secuService;
public RoleProviderPerso()
{
this.secuService = DependencyResolver.Current.GetService<IsecService>();
}
public override string[] GetRolesForUser(string username)
{
Logger.Debug("GetRolesForUser(" + username + ")", new StackTrace().GetFrame(0));
int nbRechercheAD = 0;
do
{
try
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "moncontroleurdedomaine");
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
IEnumerable<GSSecurity> listegs = secuService.allGSs();
if (user != null)
{
List<GroupPrincipal> listeAD = user.GetGroups().Select(x => (GroupPrincipal)x).ToList();
return listegs.Where(y => listeAD.Where(z => z.Name.Equals(y.gsSecurityName)).Count() > 0)
.Select(x => x.actionSecurity.actionSecurityName).ToArray();
}
}
catch (Exception e)
{
Logger.Error("GetRolesForUser(" + username + ") essai " + (nbRechercheAD + 1) + ":" + e.StackTrace, new StackTrace().GetFrame(0));
Thread.Sleep(1000);
}
} while (++nbRechercheAD < 3);
return null;
}
public override bool IsUserInRole(string username, string roleName)
{
Logger.Debug("IsUserInRole('" + username + "' , '" + roleName + "')", new StackTrace().GetFrame(0));
int nbRechercheAD = 0;
do
{
try
{
if (username == null || roleName == null) return false;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "moncontroleurdedomaine");
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if (user == null) return false;
return secuService.findActionByRoleName(roleName).gsSecurities
.Where(gs => user.IsMemberOf(GroupPrincipal.FindByIdentity(ctx, gs.gsSecurityName))).Count() > 0;
}
catch (Exception e)
{
Logger.Error("IsUserInRole('" + username + "' , '" + roleName + "')essai " + (nbRechercheAD + 1) + ":" + e.StackTrace, new StackTrace().GetFrame(0));
Thread.Sleep(1000);
}
} while (++nbRechercheAD < 3);
return false;
} |
Partager