WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity; How to make this cast work; currently it is failing

微笑、不失礼 提交于 2019-12-13 02:28:40

问题


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

回答1:


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?




回答2:


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



来源:https://stackoverflow.com/questions/983443/windowsidentity-winid-windowsidentityhttpcontext-current-user-identity-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!