简体   繁体   English

如何让我的代码在模拟时访问程序集

[英]How to let my code access assembly while impersonating

I'm trying to read images on a shared folder using WindowsIdentity.RunImpersonated .我正在尝试使用WindowsIdentity.RunImpersonated读取共享文件夹上的图像。 The problem is that during impersonation, my code can't access the System.Drawing.Common assembly, probably because the impersonated user has no access on it, what can I do.问题是在模拟过程中,我的代码无法访问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();
                }
            }
        });

This is the error I get:这是我得到的错误:

FileLoadException: Could not load file or assembly 'System.Drawing.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. FileLoadException:无法加载文件或程序集“System.Drawing.Common,Version=5.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。 Access is denied.访问被拒绝。

This worked for me case anyone faced this problem Add this line of code before WindowsIdentity.RunImpersonated这对我有用,任何人都遇到这个问题在 WindowsIdentity.RunImpersonated 之前添加这行代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码没有使用“注册日期”的程序集,有没有我没有使用的程序集? (使用日期选择器) - My code isn't using assembly for “register date”, is there an assembly i'm not using? (using datepicker) 执行代码优先迁移时无法在Visual Studio 2017中加载文件或程序集'System.ComponentModel.DataAnnotations' - Could not load file or assembly 'System.ComponentModel.DataAnnotations' in Visual Studio 2017 while doing Code First Migrations 如何从视图的 javascript 访问代码隐藏文件中的 JsonResult 变量? Razor 页。 C# - How can I access a JsonResult variable in my code behind file from the javascript of my view? Razor Pages. C# 如何访问具有不同架构的旧表? - How access to my old tables with different schema? 如何从其他razorpages访问razorpages中的代码 - How to access code in razorpages from other razorpages 不会让我在我的 cshtml 页面上拉我的列表 - wont let me pull my list on my cshtml page 我的应用程序(在 Docker 容器中)如何访问 MongoDb(在我的主机上)? - How can my application (in a Docker container) access MongoDb (on my host)? EF 核心不会让我访问同一个属性两次 - EF core won't let me access the same property twice 在 asp.net core 中模拟用户 - Impersonating user in asp.net core 如何取消任务但让她完成块? - How to cancel task but let her to finish the chunk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM