Utilisateur.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.DirectoryServices;
  3. using System.Web;
  4. namespace CD67.ModeleMVC.MVC.Internal
  5. {
  6. public static class Utilisateur
  7. {
  8. /// <summary>
  9. /// Retourne une chaine prise dans l'AD correspondant au paramètre donné à la fonction :
  10. /// prenomnom , nom , prenom , email , login
  11. /// Une fois récupéré, la valeur est stocké en variable de session, et ne sera récupéré à nouveau depuis l'AD que si nécessaire.
  12. /// Si le paramètre ne correspond à aucun de ces cas, la fonction retourne une erreur.
  13. /// </summary>
  14. /// <param name="paramAD"></param>
  15. /// <returns>"Prenom Nom" ou "NOM" ou "Prenom" ou "email" ou "login"</returns>
  16. public static string getInfoUtilisateur(string paramAD)
  17. {
  18. HttpContext context = HttpContext.Current;
  19. string resultat = "";
  20. string sessionIndex = "UserAD-" + paramAD;
  21. if (context.Session[sessionIndex] == null)
  22. {
  23. try
  24. {
  25. DirectoryEntry userEntry = getUserEntry();
  26. resultat = userEntry.Properties[paramAD].Value.ToString();
  27. context.Session[sessionIndex] = resultat;
  28. }
  29. catch (NullReferenceException ex)
  30. {
  31. throw new NullReferenceException("Paramètre AD non reconnu", ex);
  32. }
  33. }
  34. else
  35. {
  36. resultat = context.Session[sessionIndex].ToString();
  37. }
  38. return resultat;
  39. }
  40. /// <summary>
  41. /// Pour chacune des info standard que sont prenom_nom, prenom, nom, email et login,
  42. /// Renvoie la valeur qui lui correspond pour l'utilisateur courrant.
  43. /// L'info renvoyée dependra du paramètre donné à la fonction, qui devra correspondre à une des occurences de l'enum InfoStandard.
  44. /// </summary>
  45. /// <param name="infoStandard"></param>
  46. /// <returns name="resultat">Retourne l'information de l'AD correspondant au paramètre donné.</returns>
  47. public static string getInfoUtilisateur(InfoStandard infoStandard)
  48. {
  49. string resultat = "";
  50. switch (infoStandard)
  51. {
  52. case InfoStandard.nom_prenom:
  53. resultat = getInfoUtilisateur("displayName");
  54. break;
  55. case InfoStandard.prenom:
  56. resultat = getInfoUtilisateur("givenName");
  57. break;
  58. case InfoStandard.nom:
  59. resultat = getInfoUtilisateur("sn").ToUpper();
  60. break;
  61. case InfoStandard.email:
  62. resultat = getInfoUtilisateur("mail");
  63. break;
  64. case InfoStandard.login:
  65. resultat = getInfoUtilisateur("sAMAccountName");
  66. break;
  67. }
  68. return resultat;
  69. }
  70. public enum InfoStandard
  71. {
  72. nom_prenom,
  73. prenom,
  74. nom,
  75. email,
  76. login
  77. }
  78. /// <summary>
  79. /// Retourne l'entrée dans l'AD de l'utilisateur courant.
  80. /// Exemple d'accès aux propriétés : userEntry.Properties["givenName"].Value
  81. /// </summary>
  82. /// <returns>Entrée utilisateur</returns>
  83. private static DirectoryEntry getUserEntry()
  84. {
  85. try
  86. {
  87. string login = HttpContext.Current.User.Identity.Name.ToLower().Replace("cg67\\", "");
  88. if (login == null) throw new Exception("Impossible de récupérer le login de l'utilisateur courant.");
  89. DirectoryEntry ldap = new DirectoryEntry("LDAP://CG67.fr/OU=Organisation,dc=CG67,dc=fr", "LectureAD", "Adlecture!");
  90. DirectorySearcher searcher = new DirectorySearcher(ldap);
  91. searcher.Filter = $"(sAMAccountName={login})";
  92. SearchResult result = searcher.FindOne();
  93. DirectoryEntry userEntry = result.GetDirectoryEntry();
  94. return userEntry;
  95. }
  96. catch (NullReferenceException ex)
  97. {
  98. throw new Exception("Le nom d'utilisateur courant n'a pas été retrouvé dans l'AD.", ex);
  99. }
  100. catch (Exception ex)
  101. {
  102. throw new Exception("Erreur dans la récupération des infos de l'utilisateur.", ex);
  103. }
  104. }
  105. }
  106. }