简体   繁体   English

如何从.NET中的.html页面检查“会话”

[英]How to check “Session” from the .html page in `.NET`

Basically I am trying to check the Session value from the .html page . 基本上,我试图从.html page检查Session值。 SO what far I done that is 所以我所做的是

In ASPX I am storing the value in session and redirect into an html page. 在ASPX中,我将值存储在session并重定向到html页面。

Session["user_id_no"] = "1234";
Response.Redirect(url);  

From HTML I am calling an REST GET method. 从HTML,我正在调用REST GET方法。

var check= $.ajax({
    url: "/loginCheck",
    type: 'GET',
    success: function (result) {
        return result;
    },
    async:false
});

REST API and Method implementation REST API和方法实现

[HttpGet]
[Route("loginCheck")]
public bool checkLoginStatus()
{
    SessionClass sc = new SessionClass();
    bool user_id_check = sc.check("user_id_no");       
    return user_id_check;
}

Session Class is implemented as 会话类实现为

public class SessionClass : System.Web.UI.Page
{
     public bool check(string sessionName)
     {
         if (Session[sessionName] == null)
             return false;
         return true;
     }
}

But I am getting exception. 但是我越来越例外了。

HResult=-2147467259 Message=Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. HResult = -2147467259 Message = Session状态只能在配置文件或Page指令中将enableSessionState设置为true时使用。 Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\\\ section in the application configuration. 还请确保在应用程序配置的\\\\部分中包含System.Web.SessionStateModule或自定义会话状态模块。

But in my web.config 但是在我的web.config

<sessionState mode="InProc" timeout="30" />

I have also went through some question 我也遇到了一些问题

  1. How to access Session variables and set them in javascript? 如何访问会话变量并在javascript中进行设置?
  2. Getting session value in javascript 在JavaScript中获取会话值
  3. 'Session' does not exist in the current context “会话”在当前上下文中不存在
  4. Session state can only be used when enableSessionState is set to true either in a configuration 会话状态只能在配置中将enableSessionState设置为true时使用

Have you ever tried this? 您曾经尝试过吗?

using System.Web.SessionState;

public class SessionClass :  IRequiresSessionState
{
     public bool check(string sessionName)
     {
         if (HttpContext.Current.Session[sessionName] == null)
             return false;
         return true;
     }
}

you need to enableSessionState in your config as exception says 您需要在配置中enableSessionState ,如异常所示

 <system.web>
      <pages enableSessionState="true" /> 
 </system.web>

check this answer for more information 检查此答案以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM