brgdotnet
asked on
How to check is a SecureString object is empty
I need to use the SecureString in my C# web application. However in looking at this class I don't see of a way to determine if a SecureString object is empty. If I know that the the String class has an IsNullOrEmpty(String) method however, SecureString does not. Does someone know of a way to determine in a SecureString object is an empty string?
https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@MlandaT, I think that is what I stated in my post.
But what about whitespace? i don't know, but surely SecureString can contain non-visible characters, such as a NewLine or something.
Hi Brady;
From Microsoft documentation
Thanks
From Microsoft documentation
The SecureString class has no members that inspect, compare, or convert the value of a SecureString. The absence of such members helps protect the value of the instance from accidental or malicious exposure. Use appropriate members of the System.Runtime.InteropServIf you have any questions on this post please post a new question.ices.Marsh al class, such as the SecureStringToBSTR method, to manipulate the value of a SecureString object.
Thanks
I accept that the only way to check if a SecureString is truly empty is by its Length property, but my first comment was more a rhetorical question, to warn the OP that Length == 0 is not a safe replacement for IsNullOrEmpty, although he should be using IsNullOrWhiteSpace. The following code shows the length of a practically useless SecureString to be correct: 10, but ten 0 chars is as good as empty, or at least would not pass IsNullOrWhiteSpace if it were a normal string:
badlen == 10, and check = false.
SecureString badPwd = new SecureString();
for (var i = 0; i < 10; i++)
{
badPwd.AppendChar((char)0);
}
var badlen = badPwd.Length;
var check = string.IsNullOrWhiteSpace(new string((char)0, 10));
badlen == 10, and check = false.
Use the length property