NextJS 不包括我的 .NET Core Webapi 提供的 HttpOnly Cookie

问题描述

我遇到了一个问题,我的 nextjs 应用程序没有通过后端创建的 HttpOnly cookie。

在我的登录机制中,我通过以下代码提供 cookie。 (后端)

Response.Cookies.Append("jwt",mytoken,new CookieOptions()
            {
                HttpOnly = true
            });

在我的 NextJS 应用程序中,我使用 redux-toolkit 来处理我的状态。 以下代码是登录用户。

export const loginUser = createAsyncThunk(
  "auth/login",async (login: ILoginProps) => {
    const response = await fetch("https://localhost:5001/api/auth/login",{
      method: "POST",headers: {
        "Content-Type": "application/json",},credentials: "include",body: JSON.stringify(login),});
    return (await response.json()) as IUser;
  }
);

我获得了成功的 200 并收到了 HttpOnly cookie。

cookie

但是 NextJS 无法“保存”cookie。 当我转到检查工具中的选项卡 cookie 时,没有设置任何 cookie。

no cookie?

当我在 redux-toolkit 中使用 Credentials: "Include" 执行授权 api 时,HttpOnly cookie 不会与 fetch 命令一起发送。

export const getMatches = createAsyncThunk<
  IAuthCall[],void,{ state: RootState }
>("auth/getAuthcall",async (_,thunkApi) => {
  const response = await fetch("https://localhost:5001/api/authcall",{
    method: "GET",headers: {
      "Content-Type": "application/json",});
  return (await response.json()) as IAuthCall[];
});

401

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)