如果User.Identity.IsAuthenticated则...对象引用未设置为对象的实例 [英] If User.Identity.IsAuthenticated Then... Object reference not set to an instance of an object

查看:69
本文介绍了如果User.Identity.IsAuthenticated则...对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个问题令我困惑.我有一个自定义类,我的所有页面都继承自该类

Ok so this problem is baffling me. I have a custom class that all my pages inherit from

Public Class Page : Inherits Page
    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'Display Admin Controls (Buttons) '
        If User.Identity.IsAuthenticated Then
           'do some stuff '
        End If
    End Sub
End Class

但是由于某种原因,我遇到了这个错误:

But for some reason I'm getting this error:

System.NullReferenceException:对象引用未设置为对象的实例.

System.NullReferenceException: Object reference not set to an instance of an object.

在此行

If User.Identity.IsAuthenticated Then

这仅在IIS7中开始发生.我已经在IIS6中使用此应用程序超过一年了,而且没有问题,这只是自从昨天迁移到IIS7以来,此异常才开始.

This only started happening in IIS7. I have been using this application in IIS6 for over a year without issue, and it's only since I migrated to IIS7 yesterday that this exception started.

推荐答案

尝试:

If User IsNot Nothing AndAlso 
   User.Identity IsNot Nothing AndAlso 
   User.Identity.IsAuthenticated Then
    'Do stuff
End If

这篇关于如果User.Identity.IsAuthenticated则...对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆