FileDownloadHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Web;
  5. using CG67.FicheCollege.Domaine;
  6. using CG67.FicheCollege.Tools;
  7. using System.IO;
  8. using System.Security.Principal;
  9. namespace CG67.FicheCollege
  10. {
  11. /// <summary>
  12. /// Gestionnaire de téléchargement de fichier avec impersonification
  13. /// </summary>
  14. public class FileDownloadHandler : IHttpHandler
  15. {
  16. #region IHttpHandler Membres
  17. public bool IsReusable
  18. {
  19. get { return true; }
  20. }
  21. /// <summary>
  22. /// Traite la requête HTTP
  23. /// </summary>
  24. /// <param name="context">Le contexte HTTP</param>
  25. public void ProcessRequest(HttpContext context)
  26. {
  27. // context.Response.Write("hello");
  28. IntPtr token = IntPtr.Zero;
  29. WindowsImpersonationContext impersonatedUser = null;
  30. //On crée un jeton avec l'utilisateur devant accèder à la liste des fichiers
  31. bool result = Impersonation.LogonUser(ConfigurationManager.AppSettings["ImpersonationUser"], ConfigurationManager.AppSettings["ImpersonationDomain"],
  32. ConfigurationManager.AppSettings["ImpersonationPassword"],
  33. LogonSessionType.Interactive,
  34. LogonProvider.Default,
  35. out token);
  36. //Si le jeton est récupéré
  37. if (result)
  38. {
  39. //On récupère l'identité Windows
  40. WindowsIdentity id = new WindowsIdentity(token);
  41. //On change d'identité
  42. impersonatedUser = id.Impersonate();
  43. //On construit le chemin et le FileInfo vers le fichier à télécharger
  44. // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
  45. // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
  46. // FileInfo file = new FileInfo(filePath);
  47. FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FileRepository"] + context.Request.QueryString["file"]);
  48. //Si le fichier existe
  49. // if (File.Exists(filePath))
  50. if (File.Exists(file.FullName))
  51. {
  52. context.Response.ClearContent();
  53. //Préparation des headers HTTP pour le téléchargement
  54. context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  55. context.Response.AddHeader("Content-Length", file.Length.ToString());
  56. string fileExtension = Path.GetExtension(file.Name).ToLower();
  57. //On passe le bon Mime Type
  58. switch (fileExtension)
  59. {
  60. case "xls":
  61. context.Response.ContentType = "application/vnd.ms-excel";
  62. break;
  63. case "xlsx":
  64. context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  65. break;
  66. default:
  67. break;
  68. }
  69. //On transmet le fichier pour téléchargement
  70. context.Response.TransmitFile(file.FullName);
  71. context.Response.End();
  72. }
  73. }
  74. // Finalement on termine le changement d'identité
  75. if (impersonatedUser != null)
  76. impersonatedUser.Undo();
  77. // On supprime le jeton
  78. if (token != IntPtr.Zero)
  79. Impersonation.CloseHandle(token);
  80. }
  81. #endregion
  82. }
  83. }