Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在ASP.NET Core和React中创建包含jwt的httponly cookie_Javascript_Asp.net_Asp.net Core - Fatal编程技术网

Javascript 无法在ASP.NET Core和React中创建包含jwt的httponly cookie

Javascript 无法在ASP.NET Core和React中创建包含jwt的httponly cookie,javascript,asp.net,asp.net-core,Javascript,Asp.net,Asp.net Core,我正在尝试在我的应用程序中实现身份验证方案。控制器,更具体地说是方法,负责检查用户的凭证并生成jwt,之后它必须将jwt放入httponly cookie,如下所示 [HttpPost] [Route("authenticate")] public async Task<IActionResult> Authenticate([FromBody] User user) { var response = await _repository

我正在尝试在我的应用程序中实现身份验证方案。控制器,更具体地说是方法,负责检查用户的凭证并生成jwt,之后它必须将jwt放入httponly cookie,如下所示

    [HttpPost]
    [Route("authenticate")]
    public async Task<IActionResult> Authenticate([FromBody] User user)
    {
        var response = await _repository.User.Authenticate(user.Login, user.Password);
        if (!response) return Forbid();

        var claims = new List<Claim>
        {
            new Claim("value1", user.Login)
        };
        string token = _jwtService.GenerateJwt(claims);

        HttpContext.Response.Cookies.Append(
            "SESSION_TOKEN",
            "Bearer " + token,
            new CookieOptions
            {
                Expires = DateTime.Now.AddDays(7),
                HttpOnly = true,
                Secure = false
            });

        return Ok();
    }
我尝试登录时得到的所有响应都是响应代码200。我很确定这与axios的设置有关。 此外,如果有人喜欢,变量“axiosLocal”包含API的baseURL

-更新1 好啊如果我没有弄错,为了从响应中设置cookie,我必须使用{withCredentials:true}选项发送所有请求。但是当我尝试这样做时,请求被CORS阻止,尽管我已经设置了CORS策略,该策略必须允许处理来自任何来源的请求

app.UseCors(builder => builder.AllowAnyHeader()
            .AllowAnyMethod()
            .AllowAnyOrigin()
            .AllowCredentials());

终于解决了。将
.SetIsOriginAllowed(host=>true)
而不是
.AllowAnyOrigin()
作为Axios请求中的一个选项,通过
{withCredentials:true}
传递给CORS设置,这对我很有帮助

app.UseCors(builder => builder.AllowAnyHeader()
            .AllowAnyMethod()
            .AllowAnyOrigin()
            .AllowCredentials());