目前,我有2种方法:

  • 当我设置web.config cookieless="UseCookies"时,我的网址看起来像:

    http://example.com/Stuff
    <sessionState timeout="60" cookieless="UseCookies"/>
  • 当我设置cookieless="true"时,我有这样的网址

    http://example.com/%28S%28uanyuxwgaviyonky0lxwq3vq%29%29/Stuff
    <sessionState timeout="60" cookieless="true"/>


  • 我可以将cookieless属性设置为动态吗?就像是
    if(/*condition*/)
    {
        sessionState .cookieless = "true";
    }
    else
    {
        sessionState .cookieless = "UseCookies";
    }
    

    这必须在Global.asax的SessionStart中的某处

    最佳答案

    这里的基本思想是您想在运行时修改WebConfig文件。我没有尝试过自己,但是出于好奇,我通过互联网搜索了This link
    根据此,您在Global.asax中的代码将类似于

    注意:这实际上将新值写入web.config

     void Application_Start(object sender, EventArgs e)
            {
                // Code that runs on application startup
                Configuration config;
                config = WebConfigurationManager.OpenWebConfiguration("~");
                SessionStateSection SessionState = config.GetSection("system.web/sessionState") as SessionStateSection;
    
                if (SessionState != null)
                {
                    SessionState.Mode = System.Web.SessionState.SessionStateMode.InProc;// changes
    
                    if (true/*condition*/)
                    {
                        SessionState.Cookieless = System.Web.HttpCookieMode.UseCookies;
                    }
                    else
                    {
                        SessionState.Cookieless = System.Web.HttpCookieMode.UseUri; // not sure about this one
                    }
                    config.Save();
                }
            }
    

    09-28 08:29