Using custom windows impersonation code in your page's codebehind will not work as FileUltimate runs out of the page's context once it's rendered. You are probably wrapping DirectoryExists validation inside your impersonation code, that's why it works there.
It's possible to impersonate at the right time so FileUltimate requests are run under this impersonated context. For example, in your project's global.asax you can do it like this:
using System;
using System.Linq;
using System.Web;
namespace YourNamespace
{
public class YourApplication : HttpApplication
{
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
var fileUltimateRelatedPaths = new[]
{
"/MyHostPage.aspx",
"/filemanager.ashx",
"/fileuploader.ashx"
};
//Check if the current request contains any
//of the FileUltimate related paths we defined above.
if (fileUltimateRelatedPaths.Any(
h => Request.FilePath
.IndexOf(h, StringComparison.OrdinalIgnoreCase) >= 0
))
{
//Do your impersonation here
//eg. ImpersonateUser()
//We are saving the info that it's a FileUltimate
//related request and that we impersonated.
//So that at the end of the request (below method),
//we can undo impersonation only when necessary.
//You can also save other variables like impersonation
//handle etc. in Context.Items so that
//you can use them later for undoing impersonation.
Context.Items["FileUltimate.Impersonated"] = true;
}
}
protected void Application_PostRequestHandlerExecute
(object send, EventArgs e)
{
if (Context.Items["FileUltimate.Impersonated"] != null)
{
//Undo impersonation here
//eg. UndoImpersonateUser()
//You can retrieve any other variables
//you saved in Context.Items
//in above function which are required
//for undoing impersonation.
}
}
}
}
You can also use the more simple web.config impersonation method. These settings will isolate the impersonation only for FileUltimate requests and will not effect your whole application.
<configuration>
.
.
.
<location path="MyHostPage.aspx">
<system.web>
<identity impersonate="true" userName="foo" password="bar" />
</system.web>
</location>
<location path="filemanager.ashx">
<system.web>
<identity impersonate="true" userName="foo" password="bar" />
</system.web>
</location>
<location path="fileuploader.ashx">
<system.web>
<identity impersonate="true" userName="foo" password="bar" />
</system.web>
</location>
.
.
.
</configuration>
In future versions, we plan to add built-in impersonation support to FileManagerRootFolder class so that you can supply credentials too in addition to Name and Location properties when you define the root folder.