| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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)
- {
- 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(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
- }
- }
|