title: Uniquely idenitfy the extension change from excel to word in asp.net C# file upload
body: <p>**I created one excel file and change the extension type=”.docx”.I try to upload.it is uploading but if content and extension type both are different. then i need to block that one i tried few codes to avoid this with magic number and mime type but magic number is same for word and excel and zip also if i change the extension then mime type also changed. So i need to identify this content with any unique identifier
     **Here I have my sample code for finding the mime type and Magic number****</p>
  
  <pre><code>   string FileExtension = FileName.Substring((FileName.LastIndexOf(".") + 1)).ToUpper();
  string MagicNumber = HexaDecimalData.Substring(0, 47);  
  string HexaDecimalData=string.Empty;
  string MimeType = MimeMapping.GetMimeMapping(_File.FileName);
  using (var fileStream = File.OpenRead(FileName))
  {
  BinaryReader Reader = new BinaryReader(fileStream);
  Reader.BaseStream.Position = 0x0; // The offset you are reading the data from  
  byte[] data = Reader.ReadBytes(0x10); // Read 16 bytes into an array  
  HexaDecimalData = BitConverter.ToString(data);
  Reader.Close();
  } switch (FileExtension)
  {
  
                                  case "XLS":
                                      if ((!MimeType.Equals("application/excel", StringComparison.OrdinalIgnoreCase) &amp;&amp; 
                                          !MimeType.Equals("application/vnd.ms-excel", StringComparison.OrdinalIgnoreCase) &amp;&amp; 
                                          !MimeType.Equals("application/x-excel", StringComparison.OrdinalIgnoreCase) &amp;&amp; 
                                          !MimeType.Equals("application/x-msexcel", StringComparison.OrdinalIgnoreCase)) || 
                                          (!MagicNumber.StartsWith("D0-CF-11-E0-A1-B1-1A-E1") ))
                                          ErrorMessage = "Invalid File";
                                      break;
                                  case "XLSX":
                                      if ((!MimeType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", StringComparison.OrdinalIgnoreCase)) || 
                                          (!MagicNumber.StartsWith("50-4B-03-04")  &amp;&amp; !MagicNumber.StartsWith("50-4B-05-06")  &amp;&amp; !MagicNumber.StartsWith("50-4B-07-08") ))
                                          ErrorMessage = "Invalid File";
                                      break;
                                  case "PDF":
                                      if ((!MimeType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase)) || (!MagicNumber.StartsWith("25-50-44-46") ))
                                          ErrorMessage = "Invalid File";
                                      break;
                                  case "DOCX":
                                      if ((!MimeType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.OrdinalIgnoreCase)) || 
                                          (!MagicNumber.StartsWith("50-4B-03-04")  &amp;&amp; !MagicNumber.StartsWith("50-4B-05-06")  &amp;&amp; !MagicNumber.StartsWith("50-4B-07-08") ))
                                          ErrorMessage = "Invalid File";
                                      break;
                                  case "DOC":
                                      if ((!MimeType.Equals("application/msword", StringComparison.OrdinalIgnoreCase)) || (!MagicNumber.StartsWith("D0-CF-11-E0-A1-B1-1A-E1") ))
                                          ErrorMessage = "Invalid File";
                                      break;
  
  
                              }
  </code></pre>