ASP .NET不会在浏览器中写入Cookie

时间:2018-12-17 19:48:30

标签: c# asp.net-mvc cookies httpcookie

我正在尝试使用HttpCookie将cookie写入浏览器,以下是我的方法

        private async Task WriteCookie (string userName) {

            var user = await UserManager.FindByNameAsync(userName);
            ClaimsIdentity cookiesIdentity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, cookiesIdentity);

            TokenResult token = await AuthenticationHelper.GetBearerTokenAsync(Request, user.UserName, user.Password);
            HttpCookie cookie = new HttpCookie(".myCookie");
            cookie.Value = token.access_token;
            cookie.Domain = ".myDomain"
            cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);

            RedirectToAction("Dashboard", "App");

        }

我读了很多文章,但是没有一种解决方案适合我的情况,值得一提的是,我已经尝试过:

  1. 删除Cookie的域部分。
  2. 将以下设置添加到web.config

    <httpCookies httpOnlyCookies="true" requireSSL="false" /> 
    
  3. 通过https运行我的网站

编辑

我使用提琴手跟踪了我的应用程序请求,我注意到此请求的确设置了带有set-cookie参数的响应头,但是当它重定向到仪表板时,cookie从浏览器中消失了。

我希望你们能给我一些解决这个问题的线索。

已解决

在调试此方法时,发现token.access_token为null,因此cookie的值设置为null,因此当浏览器重定向到仪表板时,由于其值为null,因此正在删除cookie,因此我检查了生成令牌的过程,但出现问题,我对其进行了修复,然后一切又重新开始工作。谢谢

0 个答案:

没有答案
相关问题