饼干很快就会死掉

发布于 2024-12-09 01:23:33 字数 2427 浏览 0 评论 0原文

我在服务器上遇到 cookie 问题。饼干很快就死了。在 localhost cookie 做得很好。但对方的陌生之处。 cookie 本身存在,但该代码看不到它

对于创建 cookie,我使用此代码

    public void SignIn(BaseUser user, bool isRememberMe)
    {
        string data = string.Format("{0},{1}, {2}", user.Id, user.Role.RoleName, user.Role.IsSuperRole);
        if (string.IsNullOrEmpty(user.UserName))
        {
            throw new ArgumentException("Error", "userName");
        }

        var ticket = new FormsAuthenticationTicket(
            1,
            user.UserName,
            DateTime.Now.ToLocalTime(),
            DateTime.Now.ToLocalTime().AddDays(30),
            isRememberMe,
            data,
            FormsAuthentication.FormsCookiePath);

        var encryptedTicket = FormsAuthentication.Encrypt(ticket);

        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Path = FormsAuthentication.FormsCookiePath,
            };

        if (FormsAuthentication.CookieDomain != null)
        {
            cookie.Domain = FormsAuthentication.CookieDomain;
        }

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

对于读取和更改 cookie,我使用此代码

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        Identity id = null;

        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            id = this.GetUserIdentity(authTicket);
            authCookie.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }
        else
        {
            id = new Identity();
        }

        var user = new Principal(id, id.RoleName);
        Context.User = user;
    }

    private Identity GetUserIdentity(FormsAuthenticationTicket ticket)
    {
        string[] userDetal = ticket.UserData.Split(Convert.ToChar(","));
        int id = int.Parse(userDetal[0]);
        string roleName = userDetal[1];
        bool isSuperRole;
        bool.TryParse(userDetal[2], out isSuperRole);
        string userName = ticket.Name;

        return new Identity(id, userName, true, isSuperRole, roleName);
    }

I have problem with cookie at server. Cookie quickly die. At localhost cookie good work. But the strangeness of the other. Itself cookie is present, but that code does not see it

For create cookie I used this code

    public void SignIn(BaseUser user, bool isRememberMe)
    {
        string data = string.Format("{0},{1}, {2}", user.Id, user.Role.RoleName, user.Role.IsSuperRole);
        if (string.IsNullOrEmpty(user.UserName))
        {
            throw new ArgumentException("Error", "userName");
        }

        var ticket = new FormsAuthenticationTicket(
            1,
            user.UserName,
            DateTime.Now.ToLocalTime(),
            DateTime.Now.ToLocalTime().AddDays(30),
            isRememberMe,
            data,
            FormsAuthentication.FormsCookiePath);

        var encryptedTicket = FormsAuthentication.Encrypt(ticket);

        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Path = FormsAuthentication.FormsCookiePath,
            };

        if (FormsAuthentication.CookieDomain != null)
        {
            cookie.Domain = FormsAuthentication.CookieDomain;
        }

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

For read and change cookie, I use this code

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        Identity id = null;

        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            id = this.GetUserIdentity(authTicket);
            authCookie.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }
        else
        {
            id = new Identity();
        }

        var user = new Principal(id, id.RoleName);
        Context.User = user;
    }

    private Identity GetUserIdentity(FormsAuthenticationTicket ticket)
    {
        string[] userDetal = ticket.UserData.Split(Convert.ToChar(","));
        int id = int.Parse(userDetal[0]);
        string roleName = userDetal[1];
        bool isSuperRole;
        bool.TryParse(userDetal[2], out isSuperRole);
        string userName = ticket.Name;

        return new Identity(id, userName, true, isSuperRole, roleName);
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文