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

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

问题描述

在默认情况下,ASP.NET会设置它的饼干用mydomain.com作为自己的基地。我想preFER让他们用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.

这是在sessionState元素上的web.config配置在<的System.Web方式>

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天全站免登陆