c# - WindowsIdentity.Impersonate in ASP.NET 随机 "Invalid token for impersonation - it cannot be duplicated"

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

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

在按下按钮后的某个时刻是这段代码:WindowsIdentity.Impersonate(userIdentity.Token)userIdentity 属于 WindowsIdentity 类型,并且之前设置为 (WindowsIdentity)User.Identity

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

当我点击这段代码时,它大约有 2/3 的时间有效,但有 1/3 的时间,我得到这个异常:无效的模拟 token - 它不能被复制。我认为最让我头疼的是为什么它有时有效但其他时间无效?在某些 session 中,它会在失败前工作几次。在其他人身上,它会立即失败。

这是堆栈跟踪:

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

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

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

at Resource_Booker.BLL.ReservationAgent.SubmitReservationRequest(Reservation reservation, Patron patron) in C:\dev\RoomRes\Resource Booker\BLL\ReservationAgent.cs:line 101

at Resource_Booker.Reserve.reserve_Click(Object sender, EventArgs e) in C:\dev\RoomRes\Resource Booker\Reserve.aspx.cs:line 474

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

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

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

这里有一个混杂因素:我无法在我的本地 Windows 7 x64 工作站上重现这个问题——尽管我的身份验证是隐式传递的,因为我使用的是本地主机——或者在 Windows 2003 32 位 IIS 6.0 环境上。它只发生在非常普通的 Windows 2008 R2 环境中。所有这些环境都是域成员。

最佳答案

基本上您所看到的不是安全问题,因为登录 session 在 TCP 连接的生命周期内由 IIS 缓存,但 HTTP 偶尔会切断需要重新验证的 TCP 连接。这将无缝且不可见地发生(由浏览器处理),但它会使 token 无效,因为登录 session 将在 TCP 连接结束时被销毁。

即为了@usr 的利益,它有时只起作用,因为登录 session 是相同的,所以 token 是相同的,所以存储在 session 中的 token 有效,因为它恰好是与 User.Identity 相同的实际 token 。这不是避免安全检查的方法,而是安全检查的实现细节。

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

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

关于c# - WindowsIdentity.Impersonate in ASP.NET 随机 "Invalid token for impersonation - it cannot be duplicated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993293/

相关文章:

c# - 页面继承

C# - 多个 LINQ 查询合并为一个?

asp.net - ASP.NET Windows身份验证不起作用/不接受凭据

asp.net - 重命名 dbo.AspNetUsers 表

jquery - Css div 选项卡示例脚本

javascript - Django 中的 AJAX 渲染 JSON

c# - 将类 <对象> 转换为类 <接口(interface)>

c# - 如何为 tabitem 标题和该 tabitem 的内容设置不同的字体?

asp.net - 为什么 ASP.NET Core 在错误响应上设置缓存控制 header ?

ajax - jQuery AJAX 请求错误状态 0