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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Use the SecureString.Length Property. If the string is empty the length will be zero.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
@MlandaT, I think that is what I stated in my post.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Brady KellyCommented:
But what about whitespace? i don't know, but surely SecureString can contain non-visible characters, such as a NewLine or something.
Fernando SotoRetiredCommented:
Hi Brady;

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.InteropServices.Marshal class, such as the SecureStringToBSTR method, to manipulate the value of a SecureString object.
If you have any questions on this post please post a new question.

Brady KellyCommented:
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:

    SecureString badPwd = new SecureString();
    for (var i = 0; i < 10; i++)
    var badlen = badPwd.Length;
    var check = string.IsNullOrWhiteSpace(new string((char)0, 10));

Open in new window

badlen == 10, and check = false.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.