获取会话cookie名称

发布于 2024-10-05 06:39:26 字数 215 浏览 0 评论 0原文

是否可以获取中等信任级别的会话 cookie 名称?下面的代码在完全信任的情况下工作,但在中等信任级别抛出安全异常。

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-10-12 06:39:26

您可以使用请求对象中的HTTP_COOKIE 服务器变量来获取请求中包含的 cookie 字符串。

string cookieString = Request.ServerVariables["HTTP_COOKIE"]

如果您想要从 web.config 获取会话 cookie 名称,为什么不在包含会话 cookie 名称appSettings 部分中添加一个简单条目呢?

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

    <sessionState cookieName="__SessionCookieName"  />        

然后您可以使用以下代码读取web.config设置值:

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

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"]; }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文