FileDownloadHandler.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. IntPtr token = IntPtr.Zero;
  28. WindowsImpersonationContext impersonatedUser = null;
  29. //On crée un jeton avec l'utilisateur devant accèder à la liste des fichiers
  30. bool result = Impersonation.LogonUser(ConfigurationManager.AppSettings["ImpersonationUser"], ConfigurationManager.AppSettings["ImpersonationDomain"],
  31. ConfigurationManager.AppSettings["ImpersonationPassword"],
  32. LogonSessionType.Interactive,
  33. LogonProvider.Default,
  34. out token);
  35. //Si le jeton est récupéré
  36. if (result)
  37. {
  38. //On récupère l'identité Windows
  39. WindowsIdentity id = new WindowsIdentity(token);
  40. //On change d'identité
  41. impersonatedUser = id.Impersonate();
  42. //On construit le chemin et le FileInfo vers le fichier à télécharger
  43. // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
  44. // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
  45. // FileInfo file = new FileInfo(filePath);
  46. FileInfo file = new FileInfo(context.Request.QueryString["file"]);
  47. //Si le fichier existe
  48. // if (File.Exists(filePath))
  49. if (File.Exists(file.FullName))
  50. {
  51. context.Response.ClearContent();
  52. //Préparation des headers HTTP pour le téléchargement
  53. context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  54. context.Response.AddHeader("Content-Length", file.Length.ToString());
  55. string fileExtension = Path.GetExtension(file.Name).ToLower();
  56. //On passe le bon Mime Type
  57. switch (fileExtension)
  58. {
  59. case "xls":
  60. context.Response.ContentType = "application/vnd.ms-excel";
  61. break;
  62. case "xlsx":
  63. context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  64. break;
  65. default:
  66. break;
  67. }
  68. //On transmet le fichier pour téléchargement
  69. context.Response.TransmitFile(file.FullName);
  70. context.Response.End();
  71. }
  72. }
  73. // Finalement on termine le changement d'identité
  74. if (impersonatedUser != null)
  75. impersonatedUser.Undo();
  76. // On supprime le jeton
  77. if (token != IntPtr.Zero)
  78. Impersonation.CloseHandle(token);
  79. }
  80. #endregion
  81. }
  82. }