2

Exploring an existing project, I just came to this code. Questions came to mind is, when and why I should use SecureString over string? What are the benefits it provides?

public interface IAuthenticationService
{
    bool Login(string username, SecureString password);
}

Note: My point of interest is to simply know the additional benefits SecureString provides over string.

Johnny
  • 8,939
  • 2
  • 28
  • 33
Mahbubur Rahman
  • 4,961
  • 2
  • 39
  • 46
  • Possible duplicate of [Is SecureString ever practical in a C# application?](https://stackoverflow.com/questions/26190938/is-securestring-ever-practical-in-a-c-sharp-application) – LukaszBalazy Feb 04 '19 at 08:16
  • One more link to look at: https://stackoverflow.com/questions/141203/when-would-i-need-a-securestring-in-net – LukaszBalazy Feb 04 '19 at 08:17
  • @LukaszBalazy Why do you think these two questions is similar? My point of interest is to simply know the additional benefits SecureString provides over string. That question is quite elaborative and the concern is also different. – Mahbubur Rahman Feb 04 '19 at 08:35
  • 1
    @MahbuburRahman at this point, it doesn't offer so many benefits and the link pointed to by Johnny explains why. The main advantage is that a debugger or memory dump can't see the contents of a `SecureString`. That's not so important on desktop applications, where you don't need a password in the first place. On public *web* applications, using HTTPS is *far* more important - it's 1M times easier to steal a password by sniffing an unenctypted connection that hacking the server itself – Panagiotis Kanavos Feb 04 '19 at 08:56
  • @MahbuburRahman it's intranet applications that may need to take a username/password and authenticate against a central service. Even then, for the last 10 years the correct way is to use federated authentication. – Panagiotis Kanavos Feb 04 '19 at 08:59
  • Possible duplicate of [When would I need a SecureString in .NET?](https://stackoverflow.com/questions/141203/when-would-i-need-a-securestring-in-net) – Sinatr Feb 04 '19 at 12:13

4 Answers4

4

The purpose is to avoid the password(or so called sensitive sting) to be stored in memory as plain text. That would make the application potentially vulnerable. However it could not be generally possible as the string at the end have to be transferred to plain text by framework itself. So what SecureString actually does is shortening that period when sensitive string is kept in the memory.

However it is kind a obsolete and not recommend anymore for the new development, link.

Johnny
  • 8,939
  • 2
  • 28
  • 33
1

As its name suggests, its main purpose is to provide security. Normally the strings/texts with sensitive information (like credit cards, passwords, etc.) that should be kept confidential are stored in SecureString variable. This string gets deleted from computer memory when no longer needed. The value of an instance of SecureString is automatically protected using a mechanism supported by the underlying platform when the instance is initialized or when the value is modified.

A SecureString object is similar to a String object in that it has a text value. However, the major difference is as follows-

String It is not possible to predict when an instance of the System.String class will be deleted from computer memory. So, if a String object contains sensitive information, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.

SecureString The value of a SecureString object is pinned in memory and it automatically provides encryption, ability to mark as read-only, safe construction by NOT allowing a constant string to be passed in

DevDotNet
  • 153
  • 8
1

Secure strings were introduced to provide more protection for storing string which needed security (e.g. passwords).

At a simple level secure string work by encrypting the content of string and storing that in the memory. However this encryption will happen only on dot net framework.

For new development using this to secure important information is not advised.

Details about the class are available at: Secure Strings

peeyush singh
  • 1,337
  • 1
  • 12
  • 23
0

As the close answers above or below (great minds think alike :)) there is alink why Secure string over string class and there is a link why you should avoid to use it.

Nazim
  • 406
  • 1
  • 6
  • 20