WindowsIdentity.Impersonate在ASP.NET中随机出现“用于模仿的无效令牌 - 它无法复制”

时间:2012-01-24 20:03:13

标签: c# asp.net ajax basic-authentication windows-identity

我有一个ASP.NET应用程序,要求用户使用基本身份验证登录其域帐户。用户可以进行选择,然后按下按钮。

按下按钮后的某个时刻是以下代码:WindowsIdentity.Impersonate(userIdentity.Token) userIdentity 的类型为 WindowsIdentity ,之前已设置为(WindowsIdentity)User.Identity

userIdentity 存储为会话变量,我认为这是因为在按下按钮后,包含此代码的页面将通过AJAX调用。

当我点击此代码时,它大约有2/3的时间,但是1/3的时间,我得到了这个例外:用于模拟的无效令牌 - 它不能重复。 I认为对我来说最大的问题是为什么它有时会起作用而不是其他时候?在某些会话中,它在失败之前会工作几次。在其他方面,它立即失败。

这是堆栈跟踪:

  

在System.Security.Principal.WindowsIdentity.CreateFromToken(IntPtr userToken)

     

在System.Security.Principal.WindowsIdentity..ctor(IntPtr userToken,String authType,Int32 isAuthenticated)

     

在System.Security.Principal.WindowsIdentity.Impersonate(IntPtr userToken)

     

在C:\ dev \ RoomRes \ Resource Booker \ BLL \ ReservationAgent.cs:第101行的Resource_Booker.BLL.ReservationAgent.SubmitReservationRequest(预订,Patron赞助人)中

     

在C:\ dev \ RoomRes \ Resource Booker \ Reserve.aspx.cs中的Resource_Booker.Reserve.reserve_Click(Object sender,EventArgs e):第474行

     

在System.EventHandler.Invoke(Object sender,EventArgs e)

     

在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

     

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)

这是一个令人困惑的因素:我无法在我的本地Windows 7 x64工作站上重现此问题 - 虽然我的身份验证在此处隐式传递,因为我使用的是localhost - 或者在Windows 2003 32位IIS 6.0环境中。它只发生在漂亮的Windows 2008 R2环境中。所有这些环境都是域成员。

1 个答案:

答案 0 :(得分:10)

基本上您所看到的不是安全问题,因为在TCP连接的生命周期中,IIS会缓存登录会话,但HTTP偶尔会切断需要重新身份验证的TCP连接。这将无缝且无形地发生(由浏览器处理)但它将使令牌无效,因为当TCP连接结束时将破坏登录会话。

即。为了@usr的利益,它有时只能起作用,因为登录会话是相同的,因此令牌是相同的,因此存储在会话中的令牌可以工作,因为它恰好是与User.Identity相同的实际令牌。这不是避免安全检查的一种方式,它是安全检查的实现细节。

您不应该在会话中存储身份 - 这是不必要的,因为它是经过身份验证的连接。

每次只使用(WindowsIdentity)User.Identity,您的问题就会消失。