ASP.NET会话Cookie - 指定基本域 [英] ASP.NET Session Cookies - specifying the base domain

查看:102
本文介绍了ASP.NET会话Cookie - 指定基本域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,ASP.NET将设置其cookie以使用mydomain.com作为其基础。我更愿意让他们使用www.mydomain.com,以便我可以有其他sub.mydomain.com子域是无cookie的。

By default, ASP.NET will set its cookies to use "mydomain.com" as their base. I'd prefer to have them use "www.mydomain.com" instead, so that I can have other "sub.mydomain.com" subdomains that are cookie-free.

我已经做了一些挖掘到Session和Cookie对象,虽然我可以找到如何设置单个cookie的域,我没有看到一个方法来设置它所有的会话cookie。

I've done some digging into the Session and Cookie objects, and while I can find how to set the domain for a single cookie, I don't see a way to set it for all Session cookies.

任何人的想法?

推荐答案

创建 ISessionIDManager ,因为您只想更改Cookie域,我们将允许默认的所有的工作。

Create a ISessionIDManager, since you only want to change the cookie domain we will let the default one do all the work.

这在< system.web> 下的sessionState元素的web.config中配置。 。

This is configured in web.config on the sessionState element under <system.web>.

<sessionState sessionIDManagerType="MySessionIDManager" />

并执行。

public class MySessionIDManager: SessionIDManager, ISessionIDManager
{   
    void ISessionIDManager.SaveSessionID( HttpContext context, string id, out bool redirected, out bool cookieAdded )
    {
        base.SaveSessionID( context, id, out redirected, out cookieAdded );

        if (cookieAdded) {
            var name = "ASP.NET_SessionId";
            var cookie = context.Response.Cookies[ name ];
            cookie.Domain = "example.com";
        }
    }
}

这篇关于ASP.NET会话Cookie - 指定基本域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆