Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use SecureString in the model in MVC application correctly

Posted on 2016-10-18
4
Medium Priority
?
264 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 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