I will be accepting user uploads on a new website in .NET Core 1.0.1. I only want to allow images, however, not just any file. How can I perform this verification in .NET Core? Is it possible to do without System.Drawing
, as it is not implemented?
This answer looks promising, but it ultimately uses System.Drawing
. Is it enough to check the content type and extension?
EDIT: The accepted answer is what I needed, but I'll share my actual implementation here. I'm doing this in .NET Core but I'm pretty sure the same code works on the full framework.
static bool IsJpeg(Stream stream)
{
using (var br = new BinaryReader(stream))
{
var soi = br.ReadUInt16();
var marker = br.ReadUInt16();
return soi == 0xd8ff && (marker & 0xe0ff) == 0xe0ff;
}
}
static bool IsPng(Stream stream)
{
using (var br = new BinaryReader(stream))
{
var soi = br.ReadUInt64();
return soi == 0x0a1a0a0d474e5089;
}
}
static bool IsGif(Stream stream)
{
using (var br = new BinaryReader(stream))
{
var soi = br.ReadUInt32();
var p2 = br.ReadUInt16();
return soi == 0x38464947 && (p2 == 0x6137 || p2 == 0x6139);
}
}