[英]C# SecureString Usage From COM DLL
I'm trying to fix some Fortify errors. 我正在尝试修复一些Fortify错误。 A class in my code () stores the password in a string.
我的代码()中的类将密码存储在字符串中。
public class Foo: IDisposable
{
public string Password
{
get;
set;
}
}
Fortify recommended that I should change the string to SecureString. Fortify建议我将字符串更改为SecureString。 That fixes the Fortify issue but after this I am unable to use this create the COM Object.
这可以解决Fortify问题,但是在此之后,我将无法使用此创建COM对象。
I would define the class like this: 我将定义这样的类:
[ComVisible(true)]
public class TestSecureString : IUnsecurePassword
{
public SecureString Password
{
get;
set;
}
string IUnsecurePassword.Password
{
get
{
if (Password == null)
return null;
IntPtr ptr = Marshal.SecureStringToBSTR(Password);
string bstr = Marshal.PtrToStringBSTR(ptr);
Marshal.ZeroFreeBSTR(ptr);
return bstr;
}
set
{
if (value == null)
{
Password = null;
return;
}
Password = new SecureString();
foreach (char c in value)
{
Password.AppendChar(c);
}
}
}
}
[ComVisible(true)]
public interface IUnsecurePassword
{
string Password { get; set; }
}
Then in C++, the IUnsecurePassword
interface would be exported like this: 然后在C ++中,将像这样导出
IUnsecurePassword
接口:
virtual HRESULT __stdcall get_Password (/*[out,retval]*/ BSTR * pRetVal ) = 0;
virtual HRESULT __stdcall put_Password (/*[in]*/ BSTR pRetVal ) = 0;
It does not prevent someone from using the IUnsecurePassword interface if he really wants to, but it raises the bar. 它并不能阻止某人确实愿意使用IUnsecurePassword接口,但是会提高标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.