What causing this “Invalid length for a Base-64 char array”

前端 未结 12 1993
走了就别回头了
走了就别回头了 2020-11-28 02:56

I have very little to go on here. I can\'t reproduce this locally, but when users get the error I get an automatic email exception notification:



        
相关标签:
12条回答
  • 2020-11-28 03:10

    This is because of a huge view state, In my case I got lucky since I was not using the viewstate. I just added enableviewstate="false" on the form tag and view state went from 35k to 100 chars

    0 讨论(0)
  • 2020-11-28 03:12

    Try this:

    public string EncodeBase64(string data)
    {
        string s = data.Trim().Replace(" ", "+");
        if (s.Length % 4 > 0)
            s = s.PadRight(s.Length + 4 - s.Length % 4, '=');
        return Encoding.UTF8.GetString(Convert.FromBase64String(s));
    }
    
    0 讨论(0)
  • 2020-11-28 03:15

    During initial testing for Membership.ValidateUser with a SqlMembershipProvider, I use a hash (SHA1) algorithm combined with a salt, and, if I changed the salt length to a length not divisible by four, I received this error.

    I have not tried any of the fixes above, but if the salt is being altered, this may help someone pinpoint that as the source of this particular error.

    0 讨论(0)
  • 2020-11-28 03:17

    I've seen this error caused by the combination of good sized viewstate and over aggressive content-filtering devices/firewalls (especially when dealing with K-12 Educational institutions).

    We worked around it by storing Viewstate in SQL Server. Before going that route, I would recommend trying to limit your use of viewstate by not storing anything large in it and turning it off for all controls which do not need it.

    References for storing ViewState in SQL Server:
    MSDN - Overview of PageStatePersister
    ASP Alliance - Simple method to store viewstate in SQL Server
    Code Project - ViewState Provider Model

    0 讨论(0)
  • 2020-11-28 03:28

    My guess is that something is either encoding or decoding too often - or that you've got text with multiple lines in.

    Base64 strings have to be a multiple of 4 characters in length - every 4 characters represents 3 bytes of input data. Somehow, the view state data being passed back by ASP.NET is corrupted - the length isn't a multiple of 4.

    Do you log the user agent when this occurs? I wonder whether it's a badly-behaved browser somewhere... another possibility is that there's a proxy doing naughty things. Likewise try to log the content length of the request, so you can see whether it only happens for large requests.

    0 讨论(0)
  • 2020-11-28 03:28

    Take a look at your HttpHandlers. I've been noticing some weird and completely random errors over the past few months after I implemented a compression tool (RadCompression from Telerik). I was noticing errors like:

    • System.Web.HttpException: Unable to validate data.

    • System.Web.HttpException: The client disconnected.---> System.Web.UI.ViewStateException: Invalid viewstate.

    and

    • System.FormatException: Invalid length for a Base-64 char array.

    • System.Web.HttpException: The client disconnected. ---> System.Web.UI.ViewStateException: Invalid viewstate.

    I wrote about this on my blog.

    0 讨论(0)
提交回复
热议问题