无法在 ASP.NET 5.0 中设置 cookie

Can't set cookie in ASP.NET 5.0

我一直在尝试为用户登录信息设置 cookie,但我一直收到这些错误。

我正在导入所有这些

using System;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

// IDE says these are all unnessary
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Http;

代码是这样的:

string token = Auth.password(username,password);

HttpCookie cookie = new HttpCookie("token",token);
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);

这里是错误

C:\xxx\my-server\Pages\Login.cshtml.cs(30,5): error CS0246: The type or namespace name 'HttpCookie' could not be found (are you missing a using directive or an assembly reference?) [C:\xxx\my-server\my-server.csproj]
C:\xxx\my-server\Pages\Login.cshtml.cs(30,29): error CS0246: The type or namespace name 'HttpCookie' could not be found (are you missing a using directive or an assembly reference?) [C:\xxx\my-server\my-server.csproj]
C:\xxx\my-server\Pages\Login.cshtml.cs(32,22): error CS1061: 'IResponseCookies' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'IResponseCookies' could be found (are you missing a using directive or an assembly reference?) [C:\xxx\my-server\my-server.csproj]

我使用命令 dotnet new razor 创建了项目。
我正在使用 vscode.
dotnet 版本是 5.0.301.

问题已通过将代码更改为:

解决
string token = Auth.password(username,password);

HttpContext.Response.Cookies.Append("token", token, 
    new CookieOptions { Expires = DateTime.Now.AddDays(30) });