在 Session_Start 中引用 Context.User.Identity 时 Global.asax 中出现 NullReferenceException

发布于 2024-09-25 10:54:16 字数 582 浏览 8 评论 0原文

当我在 ASP.NET 中调试应用程序时,大约 5 分钟后,我收到来自 global.asax.cs 的 NullRefEx:

protected void Session_Start(Object sender, EventArgs e)
        {
            WindowsIdentity identity = null;
            identity = (WindowsIdentity)(Context.User.Identity);
...

此时上下文为 Null。对于 Global.asax.cs,我是 ASP.NET 的菜鸟。该代码是由另一位开发人员编写的。

编辑: 这是带有 HttpContext 的代码...

    protected void Session_Start(Object sender, EventArgs e)
    {
    WindowsIdentity identity = null;
    identity = (WindowsIdentity)(HttpContext.Current.User.Identity);

When I am debugging an application in ASP.NET, after about ~5 minutes I get a NullRefEx coming from global.asax.cs regarding:

protected void Session_Start(Object sender, EventArgs e)
        {
            WindowsIdentity identity = null;
            identity = (WindowsIdentity)(Context.User.Identity);
...

Context is Null at this point. I am a noob in ASP.NET with regards to Global.asax.cs. This code was written by another developer.

EDIT:
This is the code with HttpContext...

    protected void Session_Start(Object sender, EventArgs e)
    {
    WindowsIdentity identity = null;
    identity = (WindowsIdentity)(HttpContext.Current.User.Identity);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-10-02 10:54:16

尝试:

identity = (WindowsIdentity)(HttpContext.Current.User.Identity); 

编辑得更清楚(我希望......)

Try:

identity = (WindowsIdentity)(HttpContext.Current.User.Identity); 

edited to be more clear (I hope...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文