views:

776

answers:

2

I am basically trying to access the network share resources from my Web Application by impersonating the logged in user. I followed this example [http://msdn.microsoft.com/en-us/library/ms998351.aspx#paght000023_impersonatingbyusingwindowsidentity], here the writer does not mention about the cast failing. When i did that cast, I got the runtime exception that the cast cannot be made. Anyone has gone through this kind of issues before?

Guidance or suggestions are higly appreciated!

Thank you

  WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
  // Start impersonating
  ctx = winId.Impersonate();
  // Now impersonating
  // Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch
{
}
finally
{
  // Revert impersonation
  if (ctx != null)
  ctx.Undo();
}
// Back to running under the default ASP.NET process identity
A: 

I am not sure what you're trying to do with impersonation so it's hard to tell you exactly how to do it but the User object in your web app is equivelent to a System.Security.Principal.IPrincipal object and not a WindowsPrincipal object.

Likewise, the User.Identity is an IIdentity and not a WindowsIdenity object.

Can you post more about what you're trying to do?

Jason Watts
Please read refer to this one : [http://msdn.microsoft.com/en-us/library/ms998351.aspx#paght000023_impersonatingbyusingwindowsidentity],
Shiva
The cast will always fail for the reasons i've mentioned.
Jason Watts
A: 

What is really in your Identity ?? Maybe it's a generic identity or some other identity - not a Windows identity as you assume it is:

string typeOfIdentity = HttpContext.Current.User.Identity.GetType().FullName;

What's the result here? That might give you more information as to what you're really dealing with here.

Marc

marc_s
Well that is pretty obvious; that is the FormsIdentity , you can just infer that from HttpContext.
Shiva