impersonation of a specific user only to particular section of code

Hi,

The web application folder has files(wmv) which has assigned a specific user and has read & execute and read permission only for security reason so that it cannot be accessed directly by url. if accessed user is prompted for crendentials.

But when that file(wmv) is accessed by my asp.net application it then also prompt(For My application anonymous access is enabled in IIS)

For this impersonation has been implemented. Also i did not wanted that impersonation is applied for every request but only when a request to file is made.

for this a handler is written to server the request to particular file.

In this handler process request impersonation as well as serving of file has been implemented .

The problem is that when debuged in visual studio the impersonation do return "TRUE" and file is server without asking for crendentials.But when run thru IIS file is not served but ask for crendentials.

I am unable to debug and am unaware of what is going wrong.

the code snippets fro processrequest method are

IntPtr token =IntPtr.Zero;

WindowsImpersonationContext impersonatedUser =null;

try

{

bool result = LogonUser("username","domainname",

"password",

LogonSessionType.Network,

LogonProvider.Default,

out token);

if (result)

{

WindowsIdentity id =newWindowsIdentity(token);

impersonatedUser = id.Impersonate();

context.Response.ClearContent();

context.Response.ContentType ="application/octet-stream";

context.Response.BufferOutput =false;

context.Response.Expires = 0;

context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

context.Response.Cache.SetNoServerCaching();

context.Response.Cache.SetNoStore();

context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);

context.Response.TransmitFile(context.Request.PhysicalPath);

context.Response.End();

}

else

{

context.Response.Write("

LogonUser failed: " +

Marshal.GetLastWin32Error().ToString());

}

}

catch (Exception e)

{

// Prevent any exceptions that occur while the thread is

// impersonating from propagating

}

finally

{

// Stop impersonation and revert to the process identity

if (impersonatedUser !=null)

impersonatedUser.Undo();

// Free the token

if (token !=IntPtr.Zero)

CloseHandle(token);

}

Thanx in advance

[4798 byte] By [ritzfizz] at [2008-1-9]
# 1

Hi ritzfizz,

Recommend posting the question on the forum http://forums.asp.net , there ASP.NET experts can give you satisfying answers.

Thanks!

FengChen-MSFT at 2007-10-2 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...