Asp.net 获取窗体身份验证网站上的当前WindowsPrincipal

Asp.net 获取窗体身份验证网站上的当前WindowsPrincipal,asp.net,authentication,Asp.net,Authentication,我遇到了一个特殊的请求:我有一个使用表单身份验证的网站,但它现在需要在某个时候获取WindowsPrincipal(Windows身份验证),以便重用它 我的第一反应是创建一个新页面,并禁用IIS对该页面的匿名访问。当我在该页面上时,Request.ServerVariables[“LOGON\u USER”]按预期为我提供当前的Windows登录名 不幸的是,Context.User仍然给我一个GenericPrincipal 你知道如何在FormsAuthentication应用程序中获取当

我遇到了一个特殊的请求:我有一个使用表单身份验证的网站,但它现在需要在某个时候获取WindowsPrincipal(Windows身份验证),以便重用它

我的第一反应是创建一个新页面,并禁用IIS对该页面的匿名访问。当我在该页面上时,
Request.ServerVariables[“LOGON\u USER”]
按预期为我提供当前的Windows登录名

不幸的是,
Context.User
仍然给我一个
GenericPrincipal


你知道如何在FormsAuthentication应用程序中获取当前的WindowsPrincipal吗?(通过询问用户密码来重新创建它不是一个选项)

找到它:
Context.Request.LogonUserIdentity
是此场景中应该使用的

如果在IIS上禁用匿名访问,它将返回发出请求的windows用户(否则将返回IIS匿名用户)

对于那些对如何重用它感兴趣的人:

lblUserName.Text = WindowsIdentity.GetCurrent().Name; 
    // will return the ASP.Net user (that's useless to us)

WindowsIdentity id = Context.Request.LogonUserIdentity;
WindowsImpersonationContext ctx = id.Impersonate();
try
{
    lblUserName.Text = WindowsIdentity.GetCurrent().Name;
        // will return the correct user

    // (...) do your stuff
}
finally
{
    ctx.Undo();
}

对我来说,LogonUserIdentity返回ASP.Net用户(即应用程序池标识)。要获取LogonUserIdentity以返回经过身份验证的用户的当前表单,您还需要做什么?(我正在使用MVC)。两者都返回相同的ASP.NET用户