C# Base64 Validation

2019-08-01 13:00发布

I am receiving a base64 encoded file in my MVC controller's action from an AJAX request. Now I want to validate the base64 encodedstring. I want to allow only PDF, JPG and PNG formats.

How can I validate the base64 encoded string on the server-side, so it will not take any other type of base64 encoded files, e.g. docx, exe etc.? I want to perform server-side validation on that regardless of Javascript validation. As you may know it may be hacked.

Below is the base64 encoded version of an exe file. And I don't want it to be allowed.

string base64BinaryStr = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAD+eQmDuhhn0LoYZ9C6GGfQ1Qds0LkYZ9A5BGnQshhn0NUHbdCxGGfQ1Qdj0LgYZ9C6GGbQIxhn0HkXOtCxGGfQ7jtW0IQYZ9B9HmHQuxhn0FJpY2i6GGfQAAAAAAAAAABQRQAATAEDAPrzPVcAAAAAAAAAAOAADwELAQYAALAAAABwAAAAYAEAABECAABwAQAAIAIAAABAAAAQAAAAAgAABAAAAAAAAAAEAAAAAAAAAACQAgAAEAAAxJwEAAIAAAAAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAANiNAgBwAQAAACACANhtAAAAAAAAAAAAAJiZAwDIHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFVQWDAAAAAAAGABAAAQAAAAAAAAAAQAAAAAAAAAAAAAAAAAAIAAAOBVUFgxAAAAAACwAAAAcAEAAKQAAAAEAAAAAAAAAAAAAAAAAABAAADgLnJzcmMAAAAAcAAAACACAABwAAAAqAAAAAAAAAAAAAAAAAAAQAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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ArdkRPC/5Z8ICeRoiNpOVUBStOANMlgv7X/B36OiPvocELTBHMnrlz7OGGtPS0EkwgGM7ovtUptSNGXUSndofOZtsN9ggQtyLXdh2YIN1I1Mh6FkgId0qPjfoHh231osB9arts9KIQQQIjEcTNIASGYssg/ivsLKLwwtF3HUH6ENd6P9bH4N93AB3BYNPcgoLdQTtsRSLBlFCBhAcFA7Crx0NVCKa2oOcuokGWJYO9FXENFBTxMQIDK/JVLR3+yw6PGFbDYOH/1MXydcRM49/8KPocxBHyOSA4mPiRWFqjTc7kehEj4IDnJShKZpYXZ4Camjj22XVYM/QAXUJgNTGdmfYJKoTDhd1VgXUY0SMMNtQKbBBt7cuZXQ8Sza4dE8suAF3xwLcf1R6akW8Cv8wVmS3s2OC65F1A97rX8GLzj5LJwTr1ggFo2S5kG8uMODk5O+sHeNXL0dLWTEG7EtY4U6XBy6ZEnEwBFWGr6C7ihTcCMcBmgAQEBIhQsELX5oEdD2FmHYmV2S3ioUqbgR/dhhDIrr+O8dy";

1条回答
SAY GOODBYE
2楼-- · 2019-08-01 13:17

As much as I know, base64 string first five characters define the mime type of the file. Therefor you may be able to filter the base64 string and check which file it is, for example you can use an extension method or anything you like which resemble this:

public static bool IsAllowedMimeType(this string base64string) 
{
    if (string.IsNullOrWhiteSpace(base64string)
    {
        return false;
    }
    string data = base64string.Substring(0,5);
    switch (data.ToUpper()) 
    {
        case "IVBOR":
            //png
            return true;
            break;
        case "/9J/4":
            //jpg
            return true;
            break;
        case "JVBER":
            //pdf
            return true;
            break;
        default:
            //other types
            return false;
    }
}

After implementing such extension method you can then go with a simple if statement:

if (base64BinaryStr.IsAllowedMimeType())
{
    //what ever you want to do when type is allowed
}
查看更多
登录 后发表回答