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
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
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.
0

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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Brady KellyCommented:
But what about whitespace? i don't know, but surely SecureString can contain non-visible characters, such as a NewLine or something.
0
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.

Thanks
0
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++)
    {
        badPwd.AppendChar((char)0);
    }
    var badlen = badPwd.Length;
    var check = string.IsNullOrWhiteSpace(new string((char)0, 10));

Open in new window


badlen == 10, and check = false.
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.