| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Web;
- using CG67.FicheCollege.Domaine;
- using CG67.FicheCollege.Tools;
- using System.IO;
- using System.Security.Principal;
- namespace CG67.FicheCollege
- {
- /// <summary>
- /// Gestionnaire de téléchargement de fichier avec impersonification
- /// </summary>
- public class FileDownloadHandler : IHttpHandler
- {
- #region IHttpHandler Membres
- public bool IsReusable
- {
- get { return true; }
- }
- /// <summary>
- /// Traite la requête HTTP
- /// </summary>
- /// <param name="context">Le contexte HTTP</param>
- public void ProcessRequest(HttpContext context)
- {
- // context.Response.Write("hello");
- IntPtr token = IntPtr.Zero;
- WindowsImpersonationContext impersonatedUser = null;
-
- //On crée un jeton avec l'utilisateur devant accèder à la liste des fichiers
- bool result = Impersonation.LogonUser(ConfigurationManager.AppSettings["ImpersonationUser"], ConfigurationManager.AppSettings["ImpersonationDomain"],
- ConfigurationManager.AppSettings["ImpersonationPassword"],
- LogonSessionType.Interactive,
- LogonProvider.Default,
- out token);
- //Si le jeton est récupéré
- if (result)
- {
- //On récupère l'identité Windows
- WindowsIdentity id = new WindowsIdentity(token);
- //On change d'identité
- impersonatedUser = id.Impersonate();
- //On construit le chemin et le FileInfo vers le fichier à télécharger
- // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
- // string filePath = Path.Combine(ConfigurationManager.AppSettings["FileRepository"], context.Request.QueryString["file"]);
- // FileInfo file = new FileInfo(filePath);
- FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FileRepository"] + context.Request.QueryString["file"]);
- //Si le fichier existe
- // if (File.Exists(filePath))
- if (File.Exists(file.FullName))
- {
- context.Response.ClearContent();
- //Préparation des headers HTTP pour le téléchargement
- context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
- context.Response.AddHeader("Content-Length", file.Length.ToString());
- string fileExtension = Path.GetExtension(file.Name).ToLower();
- //On passe le bon Mime Type
- switch (fileExtension)
- {
- case "xls":
- context.Response.ContentType = "application/vnd.ms-excel";
- break;
- case "xlsx":
- context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- break;
- default:
- break;
- }
- //On transmet le fichier pour téléchargement
- context.Response.TransmitFile(file.FullName);
- context.Response.End();
- }
- }
- // Finalement on termine le changement d'identité
- if (impersonatedUser != null)
- impersonatedUser.Undo();
- // On supprime le jeton
- if (token != IntPtr.Zero)
- Impersonation.CloseHandle(token);
-
- }
- #endregion
- }
- }
|