[英]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.