Solved

How to use SecureString in the model in MVC application correctly

Posted on 2016-10-18
4
48 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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 …
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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