在Forms-Authentication中动态使用cookie

Byy*_*yyo 5 c# asp.net forms-authentication session-cookies

目前我有两种方式:

  1. 当我设置我的web.config cookieless="UseCookies"我的网址看起来:

    http://example.com/Stuff

    <sessionState timeout="60" cookieless="UseCookies"/>

  2. 当我设置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";
}
Run Code Online (Sandbox Code Playgroud)

这必须是SessionStartGlobal.asax中的某个地方

Kan*_*XXX 0

这里的基本想法是你想WebConfig在运行时修改文件。我没有尝试过自己,但出于好奇,我在互联网上搜索,并找到了这个链接 ,根据这个你的代码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();
            }
        }
Run Code Online (Sandbox Code Playgroud)