C#簽名:
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部int LsaRetrievePrivateData(
IntPtr PolicyHandle,
ref LSA_UNICODE_STRING KeyName,
out IntPtr PrivateData
);
VB簽名:
私有聲明函數(shù)LsaRetrievePrivateData Lib“ ADVAPI32.dll”(ByVal PolicyHandle盡可能長(zhǎng),ByRef KeyName就像LSA_UNICODE_STRING,ByVal私有數(shù)據(jù)一樣長(zhǎng))
用戶定義的類型:
私有類型LSA_UNICODE_STRING
長(zhǎng)度為整數(shù)'WORD
MaximumLength為整數(shù)'WORD
緩沖區(qū)為long'PWCHAR-指針
結(jié)束類型
備用托管API:
你認(rèn)識(shí)一個(gè)嗎 請(qǐng)貢獻(xiàn)它!
筆記:
有關(guān)自定義封送程序的信息,請(qǐng)參見LsaOpenPolicy。
提示與技巧:
不要使用LSA私有數(shù)據(jù)功能。而是使用CryptProtectData和CryptUnprotectData函數(shù)。
(但是,當(dāng)您需要修改COM Runas Passwords時(shí),這些將不起作用。)
樣例代碼:
使用系統(tǒng);
使用System.Text;
使用System.Runtime.InteropServices;
命名空間pinvoke.lsautil.lsautil
{
公共類LSAutil
{
[StructLayout(LayoutKind.Sequential)]
私有結(jié)構(gòu)LSA_UNICODE_STRING
{
public UInt16 Length;
公共UInt16 MaximumLength;
公共IntPtr緩沖區(qū);
}
[StructLayout(LayoutKind.Sequential)]
私有結(jié)構(gòu)LSA_OBJECT_ATTRIBUTES
{
public int Length;
公共IntPtr RootDirectory;
公共LSA_UNICODE_STRING ObjectName;
公共uint屬性;
公共IntPtr SecurityDescriptor;
公共IntPtr SecurityQualityOfService;
}
私人枚舉LSA_AccessPolicy:長(zhǎng)
{
POLICY_VIEW_LOCAL_INFORMATION = 0x00000001L,
POLICY_VIEW_AUDIT_INFORMATION = 0x00000002L,
POLICY_GET_PRIVATE_INFORMATION = 0x00000004L,
POLICY_TRUST_ADMIN = 0x00000008L,
POLICY_CREATE_ACCOUNT = 0x00000010L,
POLICY_CREATE_SECRET = 0x00000020L,
POLICY_CREATE_PRIVILEGE = 0x00000040L,
POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080L,
POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100L,
POLICY_AUDIT_LOG_ADMIN = 0x00000200L,
POLICY_SERVER_ADMIN = 0x00000400L,
POLICY_LOOKUP_NAMES = 0x00000800L,
POLICY_NOTIFICATION = 0x00001000L
}
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部uint LsaRetrievePrivateData(
IntPtr PolicyHandle,
ref LSA_UNICODE_STRING KeyName,
out IntPtr PrivateData
);
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部uint LsaStorePrivateData(
IntPtr policyHandle,
ref LSA_UNICODE_STRING KeyName,
ref LSA_UNICODE_STRING PrivateData
);
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部uint LsaOpenPolicy(
ref LSA_UNICODE_STRING系統(tǒng)名稱,
ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
uint DesiredAccess,
在IntPtr PolicyHandle中
);
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部uint LsaNtStatusToWinError(
uint status
);
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
私有靜態(tài)外部uint LsaClose(
IntPtr policyHandle
);
[DllImport(“ advapi32.dll”,SetLastError = true,PreserveSig = true)]
專用靜態(tài)外部uint LsaFreeMemory(
IntPtr buffer
);
私有LSA_OBJECT_ATTRIBUTES objectAttributes;
私有LSA_UNICODE_STRING本地系統(tǒng);
私有LSA_UNICODE_STRING secretName;
公共LSAutil(字符串密鑰)
{
如果(key.Length == 0)
{
拋出新的Exception(“密鑰長(zhǎng)度為零”);
}
objectAttributes = new LSA_OBJECT_ATTRIBUTES();
objectAttributes.Length = 0;
objectAttributes.RootDirectory = IntPtr.Zero;
objectAttributes.Attributes = 0;
objectAttributes.SecurityDescriptor = IntPtr.Zero;
objectAttributes.SecurityQualityOfService = IntPtr.Zero;
localsystem =新的LSA_UNICODE_STRING();
localsystem.Buffer = IntPtr.Zero;
localsystem.Length = 0;
localsystem.MaximumLength = 0;
secretName =新的LSA_UNICODE_STRING();
secretName.Buffer = Marshal.StringToHGlobalUni(key);
secretName.Length =(UInt16)(key.Length * UnicodeEncoding.CharSize);
secretName.MaximumLength =(UInt16)(((key.Length + 1)* UnicodeEncoding.CharSize);
}
私有IntPtr GetLsaPolicy(LSA_AccessPolicy訪問)
{
IntPtr LsaPolicyHandle;
uint ntsResult = LsaOpenPolicy(ref this.localsystem,ref this.objectAttributes,(uint)access,out LsaPolicyHandle);
uint winErrorCode = LsaNtStatusToWinError(ntsResult);
如果(winErrorCode!= 0)
{
拋出新的Exception(“ LsaOpenPolicy失?。骸?+ winErrorCode);
}
return LsaPolicyHandle;
}
私有靜態(tài)void ReleaseLsaPolicy(IntPtr LsaPolicyHandle)
{
uint ntsResult = LsaClose(LsaPolicyHandle);
uint winErrorCode = LsaNtStatusToWinError(ntsResult);
如果(winErrorCode!= 0)
{
拋出新的Exception(“ LsaClose失?。骸?+ winErrorCode);
}
}
私有靜態(tài)無(wú)效FreeMemory(IntPtr Buffer)
{
uint ntsResult = LsaFreeMemory(Buffer);
uint winErrorCode = LsaNtStatusToWinError(ntsResult);
if(winErrorCode!= 0)
{
拋出新的Exception(“ LsaFreeMemory failed:” + winErrorCode);
}
}
public void SetSecret(string value)
{
LSA_UNICODE_STRING lusSecretData = new LSA_UNICODE_STRING();
if(value.Length> 0)
{
//創(chuàng)建數(shù)據(jù)和鍵
lusSecretData.Buffer = Marshal.StringToHGlobalUni(value);
lusSecretData.Length =(UInt16)(value.Length * UnicodeEncoding.CharSize);
lusSecretData.MaximumLength =(UInt16)(((value.Length + 1)* UnicodeEncoding.CharSize);
}
else
{
//刪除數(shù)據(jù)和鍵
lusSecretData.Buffer = IntPtr.Zero;
lusSecretData.Length = 0;
lusSecretData.MaximumLength = 0;
}
IntPtr LsaPolicyHandle = GetLsaPolicy(LSA_AccessPolicy.POLICY_CREATE_SECRET);
uint結(jié)果= LsaStorePrivateData(LsaPolicyHandle,ref secretName,ref lusSecretData);
ReleaseLsaPolicy(LsaPolicyHandle);
uint winErrorCode = LsaNtStatusToWinError(result);
如果(winErrorCode!= 0)
{
拋出新的Exception(“ StorePrivateData失敗:” + winErrorCode);
}
}
public string GetSecret()
{
IntPtr PrivateData = IntPtr.Zero;
IntPtr LsaPolicyHandle = GetLsaPolicy(LSA_AccessPolicy.POLICY_GET_PRIVATE_INFORMATION);
uint ntsResult = LsaRetrievePrivateData(LsaPolicyHandle,ref secretName,out PrivateData);
ReleaseLsaPolicy(LsaPolicyHandle);
uint winErrorCode = LsaNtStatusToWinError(ntsResult);
如果(winErrorCode!= 0)
{
拋出新的Exception(“ RetreivePrivateData失?。骸?+ winErrorCode);
}
LSA_UNICODE_STRING lusSecretData =
(LSA_UNICODE_STRING)Marshal.PtrToStructure(PrivateData,typeof(LSA_UNICODE_STRING));
字符串值= Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0,lusSecretData.Length / 2);
FreeMemory(PrivateData);
返回值
}
}
}