ASP.NET MVC-设置Cookie在Chrome中不起作用 [英] ASP.NET MVC - setting a cookie not working in chrome

查看:550
本文介绍了ASP.NET MVC-设置Cookie在Chrome中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户登录时设置cookie.该cookie在Safari上可以使用,但是在Chrome上,存储/cookie"部分中没有显示该cookie.

I'm trying to set a cookie when my user logs in. The cookie works on Safari, but on Chrome it isn't showing up in the Storage / Cookies section.

我在做什么错了?

在AccountController中,我有以下内容:

In the AccountController I have the following:

    public async Task<ActionResult> Login(LoginViewModel model, string ReturnUrl)
    {

        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: true);
        switch (result)
        {
            case SignInStatus.Success:
                {
                    //create a cookie
                    HttpCookie myCookie = new HttpCookie("site");

                    //Add key-values in the cookie
                    myCookie.Values.Add("test", "test value");
                    myCookie.Domain = "local";
                    myCookie.Path = "/";

                    myCookie.Expires = DateTime.Now.AddYears(1);

                    Response.AppendCookie(myCookie);
                    return RedirectToLocal(ReturnUrl);
                }
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "The user name or password provided is incorrect.");

                return View(model);
        }
    }

更新-如果我删除了域,则cookie开始在Chrome中运行(但无法共享)

Update - if I remove the domain, then the cookie starts working in Chrome (but can't be shared)

推荐答案

由于@tiesonT,发现了问题.保存cookie时,.local是Chrome的受限制域名.该代码可以在暂存/生产中很好地工作,但是为了进行开发,我不得不更改为其他非通用域名.

Found the issue thanks to @tiesonT. .local is a restricted domain name for Chrome when saving cookies. The code works well in staging/prod, but for development I had to change to a different non generic domain name.

这似乎仅是Chrome的问题

This only appears to be an issue for Chrome

这篇关于ASP.NET MVC-设置Cookie在Chrome中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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