WindowsIdentity winId = (WindowsIdentity)HttpConte

2019-07-24 20:53发布

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

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-24 21:30

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

查看更多
相关推荐>>
3楼-- · 2019-07-24 21:49

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?

查看更多
登录 后发表回答