【问题标题】:How to let my code access assembly while impersonating如何让我的代码在模拟时访问程序集
【发布时间】:2021-04-21 18:50:14
【问题描述】:

我正在尝试使用WindowsIdentity.RunImpersonated 读取共享文件夹中的图像。问题是在模拟过程中,我的代码无法访问System.Drawing.Common 程序集,可能是因为被模拟的用户无权访问它,我该怎么办。

WindowsIdentity.RunImpersonated(safeAccessTokenHandle,() => {
            string[] files = Directory.GetFiles(path, pattern);

            if (files.Length > 0)
            {
                foreach (string file in files)
                {
                    string[] f = file.Split('.');
                    FileStream fstream = new FileStream(file, FileMode.Open);
                    Bitmap bitmap = new Bitmap(fstream);
                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms, ImageFormat.Jpeg);
                    byte[] byteImage = ms.ToArray();
                    model.Archive.Add(Convert.ToBase64String(byteImage));
                    fstream.Close();
                }
            }
        });

这是我得到的错误:

FileLoadException:无法加载文件或程序集“System.Drawing.Common,Version=5.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。访问被拒绝。

【问题讨论】:

    标签: asp.net-core filestream assemblies impersonation


    【解决方案1】:

    如果有人遇到这个问题,这对我有用 在 WindowsIdentity.RunImpersonated 之前添加这行代码

    Assembly.Load("System.Drawing.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
    

    【讨论】:

    • 这对我有用。只有我必须加载 6 个程序集才能让我的模拟正常运行。我使用 try/catch 最终找到了所有我需要的。
    猜你喜欢
    • 2011-01-19
    • 2020-12-03
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2015-02-24
    相关资源
    最近更新 更多