Asp.net HttpContext.Current.Session在Global.asax中为空
我使用表单身份验证。 在ldapaauthentication.cs中,我有属性Asp.net HttpContext.Current.Session在Global.asax中为空,asp.net,session-variables,global-asax,Asp.net,Session Variables,Global Asax,我使用表单身份验证。 在ldapaauthentication.cs中,我有属性 public static string ReturnProject { get { return HttpContext.Current.Session["Project"].ToString(); } } 在global.asax.cs中,我试图从ldapaauthentication.cs获取会话[“Project”]以
public static string ReturnProject
{
get
{
return HttpContext.Current.Session["Project"].ToString();
}
}
在global.asax.cs中,我试图从ldapaauthentication.cs获取会话[“Project”]以进行检查,并根据会话[“Project”]中的rusult将其重定向到其他页面,但我得到了System.NullReferenceException。我在ldapaauthentication.cs中选中了会话[“项目”]-可以吗
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (Request.AppRelativeCurrentExecutionFilePath == "~/")
{
if (LdapAuthentication.ReturnProject == "Team Leader")
HttpContext.Current.RewritePath("~/TLPage.aspx");
else
if (LdapAuthentication.ReturnName == "ccobserver")
HttpContext.Current.RewritePath("~/ScheduleReport.aspx");
else
HttpContext.Current.RewritePath("~/PersonPage.aspx");
}
}
无论哪个处理程序使用Application\u AcquisiteRequestState还是Application\u AuthenticateRequest
谢谢 您声明了
ReturnProject
静态属性,而HttpContext
是的实例属性,在Global.asax
中实现
静态属性无法访问实例属性,因此从ReturnProject
中删除Static
修饰符应该可以解决此问题
编辑
现在我明白了,ReturnProject
属性在ldapaauthentication.cs中声明,而不是在Global.asax中声明
让我解释一下。每次请求到达服务器时,都会创建HttpApplication
的新实例(因此,HttpContext
实例属性)。通过这种方式,您可以访问请求
和会话
——它们被绑定到与具体用户关联的具体请求。相关答复:
您似乎试图实现的是以一种很好的方式将一些会话变量(Project
)传递给ldapaauthentication
类。实现这一点的方法不止一种,但是不查看ldapaauthentication
类的源代码的明显方法是将ldapaauthentication
实例字段添加到Global.asax,通过构造函数或属性初始值设定项传递会话变量。大概是这样的:
ldapaauthentication.cs
/// <summary>
/// Creates instance of LdapAuthentication with Project value initialized
/// </summary>
public LdapAuthentication(string project) {
this.ReturnProject = project;
}
我怀疑该事件处理程序是否存在会话。你想干什么?我怀疑这是一个,你应该描述你真正想要发生的事情。您只是想在登录后重定向某人吗?另外,您需要先检查
Session[“Project”]
是否为null,然后才能ToString()
it。我正在尝试从ldapaauthentication.cs获取Session[“Project”]以进行检查,并根据Session[“Project”]中的rusult将其重定向到其他页面,但我有System.NullReferenceException。我在LdapAuthentication.cs中选中了Session[“Project”]-可能与尝试应用此测试重复:if(System.Web.HttpContext.Current.Session!=null),因为在那里可以多次调用它,并且在某些情况下会话可以为null。这就是我的工作方式。谢谢,但如果我从属性中删除静态修饰符,我可以从Global.asax在ldapaauthentication.cs中访问此属性:错误非静态字段、方法或属性库需要对象引用,类似错误System.NullReferenceException for private LdapAuthentication auth=new LdapAuthentication(HttpContext.Current.Session[“Project”].ToString());
private LdapAuthentication auth = new LdapAuthentication(
HttpContext.Current.Session["Project"].ToString()
);
...
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (Request.AppRelativeCurrentExecutionFilePath == "~/")
{
if (auth.ReturnProject == "Team Leader")
HttpContext.Current.RewritePath("~/TLPage.aspx");
else
if (auth.ReturnName == "ccobserver")
HttpContext.Current.RewritePath("~/ScheduleReport.aspx");
else
HttpContext.Current.RewritePath("~/PersonPage.aspx");
}
}