I'm doing a feature on a system where; Anyone will upload an important file, this file contains sensitive information linked to each employee of the company. Each employee can open his / her own file, but no one else can open it.
The file is in PDF format, and all follow a rule of nomenclature eg:
ano-mes-codigofuncionario.pdf
.
What happens ... when I allow the employee to download his file, he will see the name and if he understands the logic of the names, he can change the employee code and view other files people.
I would then, when saving the file to disk, generate a hash based on the file name policies and when I need to identify the file to assign to each employee I could "decrypt" and get the original name.
The final result should be something like: fe415d322sefe185d32sd1f51000e1fea6e.pdf
, this way it will be harder for other employees to try to view other files.
Here I saved to disk:
private void SalvarArquivo(HttpPostedFile file)
{
var pathString = DiretorioTemp();
var fileName1 = Path.GetFileName(file.FileName);
bool isExists = Directory.Exists(pathString);
if (!isExists)
Directory.CreateDirectory(pathString);
var path = string.Format("{0}\{1}", pathString, file.FileName);
file.SaveAs(path);
}
After reading the file I click on the button for the user, respecting the rules that are used to name the original file.
public static void Download(string fName)
{
FileInfo fInfo = new FileInfo(fName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fInfo.Name + "\"");
HttpContext.Current.Response.AddHeader("Content-Length", fInfo.Length.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(fInfo.FullName);
fInfo = null;
}
Note: If I generate a hash and can not undo it, it will not work because I will not know the parameters used when the files were inserted.
Remembering that windows does not allow some characters (* / \ < >) for this reason I would like something simple.