FicheCollege.aspx.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.Xml;
  14. using CG67.FicheCollege.Service;
  15. using CG67.FicheCollege.Domaine;
  16. using System.Collections.Generic;
  17. using System.DirectoryServices;
  18. using System.Linq;
  19. using System.Text.RegularExpressions;
  20. //using System.DirectoryServices.AccountManagement;
  21. namespace CG67.FicheCollege
  22. {
  23. public partial class FicheCollege : System.Web.UI.Page
  24. {
  25. public string user;
  26. protected void Page_Load(object sender, EventArgs e)
  27. {
  28. //Intialisation des paramètres
  29. //l'année en cours est déclarée dans le fichier de configuration
  30. int annee = int.Parse(ConfigurationManager.AppSettings["AnneeEnCours"]);
  31. HttpContext.Current.Session["Annee"] = annee;
  32. HttpContext.Current.Session["AnneeMoins1"] = annee - 1;
  33. HttpContext.Current.Session["RNE"] = Request["RNE"];
  34. string domain = "DC=cg67,DC=fr";
  35. //string sUser = ConfigurationManager.AppSettings["USER-LDAP"];
  36. //string sMdp = ConfigurationManager.AppSettings["MDP-LDAP"];
  37. using (var rootEntry = new DirectoryEntry("LDAP://CG67/" + domain, null, null, AuthenticationTypes.Secure))
  38. {
  39. using (var directorySearcher = new DirectorySearcher(rootEntry, String.Format("(sAMAccountName={0})", HttpContext.Current.User.Identity.Name.Replace("CG67\\",""))))
  40. {
  41. var searchResult = directorySearcher.FindOne();
  42. if (searchResult != null)
  43. {
  44. using (var userEntry = searchResult.GetDirectoryEntry())
  45. {
  46. string cnUser = userEntry.Properties["distinguishedName"].Value.ToString();
  47. List<string> Groups = new List<string>();
  48. Groups = cnUser.Split(new[] {"," } , StringSplitOptions.None).ToList();
  49. var rx = new Regex("OU=[AEFGHIJKLMP]{1}\\-[A-Z]{3,5}", RegexOptions.IgnoreCase);
  50. user = "AUTRE";
  51. var mission = Groups.Where(x => rx.IsMatch(x)).FirstOrDefault();
  52. if (!string.IsNullOrEmpty(mission)) user = mission.Replace("OU=","");
  53. if (user == "P-CAB")
  54. {
  55. var elu = Groups.Where(x => x.Contains("OU=P2-ELUS")).FirstOrDefault();
  56. if (!string.IsNullOrEmpty(elu)) user = elu.Replace("OU=", "");
  57. }
  58. }
  59. }
  60. }
  61. }
  62. //Traitement
  63. XmlDocument entete = new XmlDocument();
  64. entete.Load(Server.MapPath(@"~/xml/entete.xml"));
  65. try
  66. {
  67. //modif car si code rne null provoque une erreur
  68. XmlNode xmlFicheCollege = ServiceFiche.GetFicheCollege(Session["RNE"].ToString(), int.Parse(Session["annee"].ToString()));
  69. this.Title = xmlFicheCollege.SelectSingleNode("college/Etablissement/NomCollegePourPresentation").InnerText;
  70. xmlFicheCollege.SelectSingleNode("college").InnerXml += entete.DocumentElement.OuterXml;
  71. this.Xml1.DocumentContent = xmlFicheCollege.OuterXml;
  72. this.DataBind();
  73. }
  74. catch (NullReferenceException nre)
  75. {
  76. Console.WriteLine("Erreur de pointeur : null", nre.Message);
  77. //Response.Redirect("/");
  78. }
  79. }
  80. }
  81. }