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

前端 未结 12 1994
走了就别回头了
走了就别回头了 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:29

    As others have mentioned this can be caused when some firewalls and proxies prevent access to pages containing a large amount of ViewState data.

    ASP.NET 2.0 introduced the ViewState Chunking mechanism which breaks the ViewState up into manageable chunks, allowing the ViewState to pass through the proxy / firewall without issue.

    To enable this feature simply add the following line to your web.config file.

    <pages maxPageStateFieldLength="4000">
    

    This should not be used as an alternative to reducing your ViewState size but it can be an effective backstop against the "Invalid length for a Base-64 char array" error resulting from aggressive proxies and the like.

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

    After urlDecode processes the text, it replaces all '+' chars with ' ' ... thus the error. You should simply call this statement to make it base 64 compatible again:

            sEncryptedString = sEncryptedString.Replace(' ', '+');
    
    0 讨论(0)
  • 2020-11-28 03:31

    In addition to @jalchr's solution that helped me, I found that when calling ATL::Base64Encode from a c++ application to encode the content you pass to an ASP.NET webservice, you need something else, too. In addition to

    sEncryptedString = sEncryptedString.Replace(' ', '+'); 
    

    from @jalchr's solution, you also need to ensure that you do not use the ATL_BASE64_FLAG_NOPAD flag on ATL::Base64Encode:

     BOOL bEncoded = Base64Encode(lpBuffer,
                        nBufferSizeInBytes,
                        strBase64Encoded.GetBufferSetLength(base64Length),
                        &base64Length,ATL_BASE64_FLAG_NOCRLF/*|ATL_BASE64_FLAG_NOPAD*/);
    
    0 讨论(0)
  • 2020-11-28 03:32
    int len = qs.Length % 4;
                if (len > 0) qs = qs.PadRight(qs.Length + (4 - len), '=');
    

    where qs is any base64 encoded string

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

    As Jon Skeet said, the string must be multiple of 4 bytes. But I was still getting the error.

    At least it got removed in debug mode. Put a break point on Convert.FromBase64String() then step through the code. Miraculously, the error disappeared for me :) It is probably related to View states and similar other issues as others have reported.

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

    This isn't an answer, sadly. After running into the intermittent error for some time and finally being annoyed enough to try to fix it, I have yet to find a fix. I have, however, determined a recipe for reproducing my problem, which might help others.

    In my case it is SOLELY a localhost problem, on my dev machine that also has the app's DB. It's a .NET 2.0 app I'm editing with VS2005. The Win7 64 bit machine also has VS2008 and .NET 3.5 installed.

    Here's what will generate the error, from a variety of forms:

    1. Load a fresh copy of the form.
    2. Enter some data, and/or postback with any of the form's controls. As long as there is no significant delay, repeat all you like, and no errors occur.
    3. Wait a little while (1 or 2 minutes maybe, not more than 5), and try another postback.

    A minute or two delay "waiting for localhost" and then "Connection was reset" by the browser, and global.asax's application error trap logs:

    Application_Error event: Invalid length for a Base-64 char array.
    Stack Trace:
         at System.Convert.FromBase64String(String s)
         at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
         at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)
         at System.Web.UI.HiddenFieldPageStatePersister.Load()
    

    In this case, it is not the SIZE of the viewstate, but something to do with page and/or viewstate caching that seems to be biting me. Setting <pages> parameters enableEventValidation="false", and viewStateEncryption="Never" in the Web.config did not change the behavior. Neither did setting the maxPageStateFieldLength to something modest.

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