I have a host which is medium trust level, im using imageresizer nuget package for my image processes. So when i'm trying to using ImageBuilder.Build i got this error:

    Access to the path '(path)' is denied.
System.UnauthorizedAccessException: Access to the path '(path)' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at ImageResizer.ImageBuilder.BuildJob(ImageJob job)
   at ImageResizer.ImageBuilder.Build(ImageJob job)
   at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource, Boolean addFileExtension)
   at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource)
   at _10oy.UI.Web.Imager.Models.Downloader.SaveAndSetDimension(String path, Int32 width, Int32 height)

btw: (path)'s edited by me.

I research error but i didn't find any effective result.

here is my code (i'm calling it in threading.timer, it can be issue ?)

    public void SaveAndSetDimension(string path, int width, int height)
    {
        try
        {
            ImageBuilder.Current.Build(_MainStream, path, new ResizeSettings() { MaxWidth = width, MaxHeight = height }, false);
        }
        catch (Exception ex)
        {
            Results.Failed++;
            DAL.Classes.Log.Write(
            message: ex.Message,
            innerexcepition: ex.InnerException != null ? ex.InnerException.ToString() : ex.ToString(),
            user: "Saver",
            interfaces: 4 //Imager
            );
        }
    }

and i tried regular file create in action

    public ContentResult CreateFile()
    {
        System.IO.File.Create(Server.MapPath("~/myfile.txt"));

        return Content("File Created");
    }

and its work.

thanks.

edit: When i call it on normal action its working great but when call in timer i got this error.

有帮助吗?

解决方案

I find it. Thread was using another windows identity because of that it can't create a file or delete or anything. I fix it like that;

Get identity from application_start etc. like this.

        identity = System.Security.Principal.WindowsIdentity.GetCurrent();

after that, call this before file process

        identity.Impersonate();

and it work.

其他提示

You say that you're calling this from a timer - is it possible you've got a previous run of the timer still around? Try ensuring you use a unique file each time the timer is run to verify that your program is not locking the file against itself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top