c# - http.context.user 和 thread.currentprincipal 之间的区别以及何时使用它们?

标签 c# asp.net security genericprincipal

我最近遇到了在 visual studio 2008 下运行 asp.net web 应用程序的问题。我收到错误“type is not resolved for member...customUserPrincipal”。追踪各种讨论组发现,当您针对 Thread.CurrentPrincipal 分配自定义主体时,Visual Studio 的 Web 服务器似乎存在问题。

在我的代码中,我现在使用...

HttpContext.Current.User = myCustomPrincipal
//Thread.CurrentPrincipal = myCustomPrincipal

我很高兴我排除了错误,但它回避了“这两种设置委托(delegate)人的方法有什么区别?”的问题。 stackoverflow还有其他questions与差异相关,但他们没有深入了解这两种方法的细节。

我确实找到了一个诱人的帖子,其中有以下夸张的评论,但没有任何解释来支持他的断言......

Use HttpConext.Current.User for all web (ASPX/ASMX) applications.

Use Thread.CurrentPrincipal for all other applications like winForms, console and windows service applications.

安全/.net 专家中的任何人都可以阐明这个主题吗?

最佳答案

HttpApplication 对象在获取线程时所做的第一件事是将线程的主体设置为 HttpContext 的主体。这会同步主体。

但是,如果您稍后去设置 Thread 的主体,则 HttpApplication 内部仍然为用户上下文设置不同的主体。这就是为什么您应该始终通过 HttpContext 设置它。

(如果您查看 Reflector,您会看到在 HttpContext.User 上执行“设置”时运行的复杂代码 - 它使用 IIS 执行大量内部操作以正确设置主体。)

关于c# - http.context.user 和 thread.currentprincipal 之间的区别以及何时使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3057937/

相关文章:

c# - 通过创建文件夹并嵌套文件夹或创建带点的文件夹名称来创建命名空间?

c# - C#如何在默认浏览器中打开

.net - session 对象不更新 ASP.NET

c# - 如何将浏览器重定向到 ASP.NET 中的本地文件?

python - Django Q 对象(复杂查询)安全吗?

javascript - Google 脚本 Gmail 诈骗?

c# - 如何在asp.net core 3.1中使用自动生成的刷新 token ?

c# - Entity Framework 加载多级关联

c# - Web API 状态端点(GET 和 POST)

jquery - 将脚本添加到单独的 .js 文件