Solved

How to use SecureString in the model in MVC application correctly

Posted on 2016-10-18
4
81 Views
Last Modified: 2016-11-28
I want to use SecureString as a input type for a user input password in the view model and been looking for examples. The password is ofcourse input via the view and posted to the server via https where the view model is reconstructed. Anyone here have any good examples?
0
Comment
Question by:itnifl
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 41849408
you can use securestring for password. suppose we are taking password in textbox1 and storing it in securestring

When the page is accessed, there is a copy of "textbox1.text" in the memory, but it is in the memory of server side.

Usually we do not have to use SecureString in ASP.NET Apps, we use Forms Authentication and SSL to secure transmission of password and confidential information


This is the HtmlHelper extension I have come up with. I am sure it can be improved, but it achieves my goal of writing a SecureString to the Response stream without it ever being represented as a string.

public static class SecureStringHelpers
{
    public static void WriteSecureStringToResponse(this HtmlHelper helper, SecureString secureString)
    {
        if (secureString != null)
        {
            IntPtr unmanagedString = IntPtr.Zero;

            var secureByteArray = new byte[2];

            try
            {
                unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);

                var offset = 0;
                var endOfString = false;

                do
                {
                    secureByteArray[0] = Marshal.ReadByte(unmanagedString, offset);
                    offset++;
                    secureByteArray[1] = Marshal.ReadByte(unmanagedString, offset);
                    offset++;

                    if (!(secureByteArray[0] == 0 && secureByteArray[1] == 0))
                    {
                        helper.ViewContext.Writer.Write(System.BitConverter.ToChar(secureByteArray, 0));
                    }
                    else
                    {
                        endOfString = true;
                    }

                } while (!endOfString);
            }
            finally
            {
                Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
                secureByteArray[0] = 0;
                secureByteArray[1] = 0;
            }
        }
    }

Open in new window

0
 
LVL 2

Author Comment

by:itnifl
ID: 41849479
Thank you. Could you give me an example on how you use it?
I see it is an extension method, but how exactly does the string input end in there and how do I use what you write to the response stream in the controller side?
0
 
LVL 2

Author Comment

by:itnifl
ID: 41858598
I ended up using a string as a get-set property with code where the set property stores the value as a private secure string. Maybe not entirely the way it should, but I guess the final result us a SecureString.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41903892
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Naman Goel (https:#a41849408)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question