【问题标题】:Get IIS User Before Impersonate And After Impersonate in ASP.NET Behind Code在代码后面的 ASP.NET 中模拟之前和模拟之后获取 IIS 用户
【发布时间】:2014-02-05 10:41:30
【问题描述】:

我在我的站点中使用模拟,并且我需要记录对站点的每个请求,所以还需要用户名,我想我们可以在服务器变量中得到这个,但是 Auth_UserLogOn_User 都返回空字符串。如何在模拟之前和模拟之后获取用户名?有人有什么想法吗?

【问题讨论】:

    标签: asp.net iis .net-4.0 impersonation


    【解决方案1】:

    使用HttpContext.Current.User.Identity

    WindowsIdentity wId = (WindowsIdentity)HttpContext.Current.User.Identity;
    WindowsIdentity wIdb4 = WindowsIdentity.GetCurrent();
    string name = wIdb4.Name;
    Response.Write("Before impersonation"+name +"<br>");// <-- Writes ASPNET Account
    
    
    //Till this line,code is executed in the context of worker process
    WindowsImpersonationContext wIdCon = wId.Impersonate();
    WindowsIdentity wIdafter = WindowsIdentity.GetCurrent();
    name = wIdafter.Name;
    Response.Write("After Impersonation " + name + "<br>");// <-- writes Logged in user
    
    //Run in the context of logged authenticated user, do your //operations that require impersonation
    
    wIdCon.Undo();
    WindowsIdentity wIdafterUndo = WindowsIdentity.GetCurrent();
    name = wIdafterUndo.Name;
    
    Response.Write("After undo Impersonation " + name + "<br>");
    
    OUTPUT
    Before impersonation SERVER\ASPNET
    After Impersonation TestAccount
    After undo Impersonation SERVER\ASPNET
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      相关资源
      最近更新 更多