【问题标题】:C# 'cannot convert from 'char[]' to 'char*'C# '无法从 'char[]' 转换为 'char*'
【发布时间】:2017-06-27 05:49:16
【问题描述】:

我在使用 C# 的进程中添加密码凭据时遇到问题

目前看起来如下:

char[] passwordChars = {'x', 'x', 'x', '@', 'x', 'x', 'x', 'x'} ;
System.Security.SecureString ssPwd = new SecureString(passwordChars, passwordChars.Length);

我想知道如何继续将它从 char[] 转换为 char*。

【问题讨论】:

  • “此 API 支持产品基础架构,不打算直接从您的代码中使用。” 可能重复使用 AppendChar。 (您在数组中有passwordChars 的事实已经部分推翻了SecureString 的观点。)
  • 请注意,SecureString 不再被认为是安全的。见here

标签: c#


【解决方案1】:

取自SecureString documentation

System.Security.SecureString ssPwd;
char[] passwordChars = {'x', 'x', 'x', '@', 'x', 'x', 'x', 'x'} ;
fixed(char* pChars = passwordChars)
{
   ssPwd = new SecureString(pChars, chars.Length);
}


//Don't forget to dispose of your string after you're done with it:
ssPwd.Dispose();

【讨论】:

  • 我刚试了一下,得到:“参数 1 可能无法使用 'ref' 关键字传递”
  • 查看我更新的代码。您可能需要将其包装在 unsafe { } 上下文中。
  • 嗯,创建安全字符串的不安全代码。听起来几乎是……诗意。
【解决方案2】:
char[] passwordChars = { 'x', 'x', 'x', '@', 'x', 'x', 'x', 'x' };
System.Security.SecureString ssPwd = new SecureString();
foreach (var c in passwordChars)
{
    ssPwd.AppendChar(c);
}
ssPwd.MakeReadOnly();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    相关资源
    最近更新 更多