public static void SaveKey(string appId, string tenantId, SecureString serviceKey)
        {
            var credential = new CredStore.NativeMethods.Credential
            {
                flags       = 0,
                type        = CredStore.CredentialType.Generic,
                targetName  = CreateKey(appId, tenantId),
                targetAlias = null,
                comment     = null,
                lastWritten = new FILETIME {
                    dwHighDateTime = 0, dwLowDateTime = 0
                },
                persist       = 2, // persist on local machine
                attibuteCount = 0,
                attributes    = IntPtr.Zero,
                userName      = keyStoreUserName
            };

            // Pull bits out of SecureString to put in credential
            IntPtr credPtr = IntPtr.Zero;

            try
            {
                credential.credentialBlob     = Marshal.SecureStringToGlobalAllocUnicode(serviceKey);
                credential.credentialBlobSize = (uint)(serviceKey.Length * Marshal.SystemDefaultCharSize);

                int size = Marshal.SizeOf(credential);
                credPtr = Marshal.AllocHGlobal(size);

                Marshal.StructureToPtr(credential, credPtr, false);
                CredStore.NativeMethods.CredWrite(credPtr, 0);
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(credPtr);
                }

                Marshal.ZeroFreeGlobalAllocUnicode(credential.credentialBlob);
            }
        }
        public static void SaveKey(string appId, string tenantId, SecureString serviceKey)
        {
            var credential = new CredStore.NativeMethods.Credential
            {
                flags = 0,
                type = CredStore.CredentialType.Generic,
                targetName = CreateKey(appId, tenantId),
                targetAlias = null,
                comment = null,
                lastWritten = new FILETIME {dwHighDateTime = 0, dwLowDateTime = 0},
                persist = 2, // persist on local machine
                attibuteCount = 0,
                attributes = IntPtr.Zero,
                userName = keyStoreUserName
            };

            // Pull bits out of SecureString to put in credential
            IntPtr credPtr = IntPtr.Zero;
            try
            {
                credential.credentialBlob = Marshal.SecureStringToGlobalAllocUnicode(serviceKey);
                credential.credentialBlobSize = (uint)(serviceKey.Length * Marshal.SystemDefaultCharSize);

                int size = Marshal.SizeOf(credential);
                credPtr = Marshal.AllocHGlobal(size);

                Marshal.StructureToPtr(credential, credPtr, false);
                CredStore.NativeMethods.CredWrite(credPtr, 0);
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(credPtr);
                }

                Marshal.ZeroFreeGlobalAllocUnicode(credential.credentialBlob);
            }
        }