0

I have to memorize the username and password and Ipaddress in a wpf applicdation and i try to do it using Credential Manager in the way given at this link: Encrypting credentials in a WPF application

It memorizes well the Username and password but never memorizes the Ipaddres which i treid to memorize. The other problem is it forget the memorization of even Usrname and password on the next day when i turn on PC. It should always remember it.

My code is here :

For writing credentials to COM :

  public static int WriteCredential(string applicationName, string userName, string secret,string ipAddress)
        {
            byte[] byteArray = Encoding.Unicode.GetBytes(secret);
            if (byteArray.Length > 512)
                throw new ArgumentOutOfRangeException("secret", "The secret message has exceeded 512 bytes.");

            CREDENTIAL credential = new CREDENTIAL(); //see below my credential structure
            credential.AttributeCount = 0;
            credential.Attributes = IntPtr.Zero;
            credential.Comment = IntPtr.Zero;
            credential.TargetAlias = IntPtr.Zero;
            credential.Type = CredentialType.Generic;
            credential.Persist = (UInt32)CredentialPersistence.Session;
            credential.CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length;
            credential.TargetName = Marshal.StringToCoTaskMemUni(applicationName);
            credential.CredentialBlob = Marshal.StringToCoTaskMemUni(secret);
            credential.UserName = Marshal.StringToCoTaskMemUni(userName ?? Environment.UserName);
            credential.IpAddress= Marshal.StringToCoTaskMemUni(ipAddress);

            bool written = CredWrite(ref credential, 0);
            int lastError = Marshal.GetLastWin32Error();

            Marshal.FreeCoTaskMem(credential.TargetName);
            Marshal.FreeCoTaskMem(credential.CredentialBlob);
            Marshal.FreeCoTaskMem(credential.UserName);
            Marshal.FreeCoTaskMem(credential.IpAddress);

            if (written)
                return 0;

            throw new Exception(string.Format("CredWrite failed with the error code {0}.", lastError));
        }

Credential struct:

  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct CREDENTIAL
    {
        public UInt32 Flags;
        public CredentialType Type;
        public IntPtr TargetName;
        public IntPtr Comment;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
        public UInt32 CredentialBlobSize;
        public IntPtr CredentialBlob;
        public UInt32 Persist;
        public UInt32 AttributeCount;
        public IntPtr Attributes;
        public IntPtr TargetAlias;
        public IntPtr UserName;
        public IntPtr IpAddress;
    }

For reading from COM:

  public static Credential ReadCredential(string applicationName)
        {
            IntPtr nCredPtr;
            bool read = CredRead(applicationName, CredentialType.Generic, 0, out nCredPtr);
            if (read)
            {
                using (CriticalCredentialHandle critCred = new CriticalCredentialHandle(nCredPtr))
                {
                    CREDENTIAL cred = critCred.GetCredential(); //cred.IpAddress gives me null here whereas it is not null in case of UserName and CredentialBlob
                    return ReadCredential(cred);
                }
            }
            return null;
        }

How to solve the two problems:

(1) How to memorize IP address

(2) How to memorize the data even next day after the next PC turn on

I found the answer for second question :CRED_PERSIST_LOCAL_MACHINE but not first First Question, The other solution for second problem i could find was doing " CredentialManager.WriteCredential("Application", userName + "+" + ipAddress, passWord);" and then split while Reading it on '+' . If somebody konw beter solution ?

Community
  • 1
  • 1
  • I found the answer for second question :CRED_PERSIST_LOCAL_MACHINE bti not first one – tosttest tosttest Mar 03 '17 at 12:37
  • The other solution i could find was doing " CredentialManager.WriteCredential("Application", userName + "+" + ipAddress, passWord);" and then split while Reading it on '+' . If somebody konw beter solution ? – tosttest tosttest Mar 03 '17 at 13:13

0 Answers0