LINQ 和剪贴板安全问题

发布于 2024-12-05 03:13:15 字数 588 浏览 0 评论 0原文

为什么需要在 LINQ 查询上调用 ToList() 方法?

例如:

private void btnEnc_Click(object sender, RoutedEventArgs e)
{
    SHA1 sha = new SHA1Managed();
    string sResult = "";

    var v = sha.ComputeHash(
        UTF8Encoding.Unicode.GetBytes(tbxWordToEncrypt.Text)
        ).Select(
            p => sResult += string.Format("{0:x2}", p)
        ).ToList();

    Clipboard.SetText(sResult);

    tbxEncrypted.Text = sResult;
}

另外,当我尝试访问剪贴板时,我会看到一个安全对话框。我怎样才能防止这种情况发生?

SilverLight ClipBoard 访问消息框

Why do I need to call the ToList() method on my LINQ query?

For example:

private void btnEnc_Click(object sender, RoutedEventArgs e)
{
    SHA1 sha = new SHA1Managed();
    string sResult = "";

    var v = sha.ComputeHash(
        UTF8Encoding.Unicode.GetBytes(tbxWordToEncrypt.Text)
        ).Select(
            p => sResult += string.Format("{0:x2}", p)
        ).ToList();

    Clipboard.SetText(sResult);

    tbxEncrypted.Text = sResult;
}

Also, when I try to access the clipboard I get a security dialog box. How can I prevent this?

SilverLight ClipBoard Access MessageBox

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-12-12 03:13:15

默认情况下,您在部分信任下运行。调用 ClipBoard.SetText()(或 ClipBoard.SetText(...))时,用户必须确认访问。

如果您创建浏览器外应用程序并请求提升信任,则此限制不再适用,并且不会显示任何对话框。

您可以将应用程序配置为需要提升的信任。您需要在应用程序的清单中进行设置。

有关详细信息,请查看 MSDN:

http:// /msdn.microsoft.com/en-us/library/ee721083(v=vs.95).aspx

  1. 打开项目的属性并导航到 Silverlight 选项卡。
  2. 选中“启用浏览器外运行应用程序”选项。
  3. 单击“浏览器外设置”按钮。将弹出一个新对话框。
  4. 选中“在浏览器外部运行时需要提升信任”选项。

当用户安装您的 Silverlight 应用程序时,他们将收到安全警告,然后才能继续。这只会发生一次。以这种方式运行应用程序时,ClipBoard.SetText() 调用将不再触发安全对话框。

By default you run under partial trust. When calling ClipBoard.SetText() (or ClipBoard.SetText(...)) the user must confirm access.

If you create an out-of-browser application and request elevated trust, this restriction no longer applies and no dialog box is shown.

You can configure your application to require elevated trust. You need to set this in the application's manifest.

For more information take a look at MSDN:

http://msdn.microsoft.com/en-us/library/ee721083(v=vs.95).aspx

  1. Open your project's properties and navigate to the Silverlight tab.
  2. Check the option "Enable running application out of browser".
  3. Click on the button Out-Of-Browser Settings. A new dialog will popup.
  4. Check the option "Require elevated trust when running outside the browser".

When a user installs your Silverlight application they will get a security warning before they can proceed. This only happens once. When running your application this way the ClipBoard.SetText() call will no longer trigger a security dialog.

情丝乱 2024-12-12 03:13:15

需要调用 list 的原因是,在计算 LINQ 语句创建的表达式之前,不会计算 Select 内的表达式。因为您使用它附加到 sResult,所以在将变量放入剪贴板之前,该变量的值不会更改,除非您使用 ToList()“运行”LINQ 表达式。请注意,ToList() 的输出基本上没有价值。

更大的问题是您滥用了选择。您确实应该使用 string.Join 而不是在 Select 子句中构建字符串。对于阅读代码的人来说,将其构建在 Select 子句中将会是意想不到的,并且更难以理解。

var sResult = string.Join( "",
                 sha.ComputeHash(
                       UTF8Encoding.Unicode.GetBytes(tbxWordToEncrypt.Text)
                 ).Select(
                       p => string.Format("{0:x2}", p)
                 ));

The reason that you need to call to list is because the expression inside the Select isn't evaluated until the expression created by the LINQ statement is evaluated. Because you're using it to append to sResult, that variable won't have had its value changed before you put it on the clipboard unless you "run" the LINQ expression using ToList(). Note that the output of ToList() is basically worthless.

The bigger problem is that you're misusing the Select. You really should be using string.Join instead of building the string inside the Select clause. Building it inside the Select clause is going to be unexpected for people reading your code and harder to understand.

var sResult = string.Join( "",
                 sha.ComputeHash(
                       UTF8Encoding.Unicode.GetBytes(tbxWordToEncrypt.Text)
                 ).Select(
                       p => string.Format("{0:x2}", p)
                 ));
甜是你 2024-12-12 03:13:15

至于Linq:

如果您希望立即计算数据,则只需调用ToList()

大多数 Linq 运算符是 懒惰 的设计,这是一件好事。

As for Linq:

You only need to call ToList() if you want the data to be evaluated imediatly.

Most Linq operators are lazy by design, and it's a good thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文