Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Greetings all.

I do not know .NET (C#), but am in the process of learning. I have a good VB and PERL scripting background and expert level understanding of classic ASP, IIS and Apache.

I've been asked to port an application from Windows 2003 IIS6 to Win 2008 IIS7. 99% of our applications are JAVA. This ASP.net app was a one off so I don't have a lot of support available to me.

Upon doing so the application failed with the resulting error:
Exception System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.


The application does a routine with user passwords to facilitate auto-login. I've done some research on the error and I'm obviously not the first one to encounter it.
Hoping someone has the patience and "know-how" to assist.

How would one modify the below routine to get around the error?

Thanks in advance for your time and knowledge!

C#
if (result != null)
{
IEnumerator enumerator = result.Properties["CacheCredVal"].GetEnumerator();
   if (enumerator.MoveNext())
   {
      passwordByteArray = (byte[])enumerator.Current;
      passwordCharArray = UTF32Encoding.ASCII.GetChars(passwordByteArray);
      password = new string(passwordCharArray);
   }
}
else
{
   throw new Exception(String.Format("Cannot get password from LDAP for user {0})", userId));
}
Posted
Updated 9-Sep-13 6:58am
v3

You cannot cast it. The whole idea is wrong: you can represent the string as raw byte data, but it depends on encoding, so the question is ambiguous: you will get different but equivalent results with different but equivalent encodings. As .NET string is a Unicode string, the encoding should be one of the UTFs: Unicode (which is actually UTF-16LE, this is a very confusing Windows jargon), UTF32, UTF8 (most recommended in most cases) and UTF7 (not recommended):
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

Other encodings (like ASCII) may cause lost of some character (non-supported characters will be replaced to '?'), so you cannot safely use them.

Note that if you need memory representation of string equivalent to Windows in-memory representation of characters, this is "Unicode", that is UTF-16LE.

And here is how you can get raw byte data from a string and a given encoding: http://msdn.microsoft.com/en-us/library/ds4kkd55.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 9-Sep-13 16:26pm    
Yes, the exception is a fair indication that the cast is impossible ...
Sergey Alexandrovich Kryukov 9-Sep-13 16:33pm    
Sure. Thank you, Espen.
—SA
Abhinav S 9-Sep-13 22:55pm    
5.
Sergey Alexandrovich Kryukov 10-Sep-13 0:30am    
Thank you, Abhinav.
—SA
Dholakiya Ankit 10-Sep-13 1:11am    
agree 5ed
If I were to guess, enumerator.Current returns a string and not a byte[] - if this is the case you already have what you want, or at least you have string.

Apart from that, you should probably do something like:
IEnumerable collection = (IEnumerable)result.Properties["CacheCredVal"];
foreach( object element in collection )
{
  string password = element.ToString();
}


It's also possible that the data is base 64 encoded, so perhaps something like this will help:
IEnumerable collection = (IEnumerable)result.Properties["CacheCredVal"];
foreach( object element in collection )
{

  byte[] encodedDataAsBytes = System.Convert.FromBase64String(element.ToString());
  string password = Encoding.Default.GetString(encodedDataAsBytes);
}


foreach gets the IEnumerator from an IEnumerable object and iterates over the collection.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Abhinav S 9-Sep-13 22:55pm    
5.
Espen Harlinn 10-Sep-13 13:45pm    
Thank you, Abhinav :-)
You could debug, step through your code and figure out exactly where the problem is, which, as indicated by the error message is a clear case of an invalid cast.

(byte[])enumerator.Current is the possible culprit where a string value is being converted into a byte array directly.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Sep-13 14:16pm    
This approach is wrong in principle. The solution is quite obvious, please see my answer.
—SA
Abhinav S 9-Sep-13 22:55pm    
I just said casting was the issue. I never mentioned whether it was the right or wrong approach :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900