Get session cookie name

南笙酒味 提交于 2019-12-23 13:39:16

问题


Is it possible to get session cookie name in medium trust level? The code below works in full trust, but throws a security exception in medium trust level.

string sessionCookieName = ((SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState")).CookieName;

回答1:


You can use HTTP_COOKIE server variable from the Request object, to get the cookie string that was included with the request.

string cookieString = Request.ServerVariables["HTTP_COOKIE"]

If what you want is to obtain the session cookie name from the web.config, why don't you add a simple entry in the appSettings section containing the session cookie name?

    <appSettings>       
        <add key="SessionCookieName" value="__SessionCookieName"/>
    <appSetting>

    <sessionState cookieName="__SessionCookieName"  />        

Then you can read the web.config setting value by using the following code:

public static bool SessionCookieName
{
    get { return ConfigurationManager.AppSettings["SessionCookieName"]; }
} 


来源:https://stackoverflow.com/questions/4310763/get-session-cookie-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!