본문 바로가기

카테고리 없음

9. next.js 쿠키 사용

1. Server Side 에서 API로 쿠키 보내기
  const strUrl = process.env.NEXT_PUBLIC_URL_API + "/todolist";
  const res = await axios.get(strUrl, { headers: context.req ? { cookie: context?.req?.headers?.cookie } : undefined })
 
2. Client Side 에서 API로 쿠키 보내기
  const strUrl = process.env.NEXT_PUBLIC_URL_API + "/todolist";
  const res = await axios.get(strUrl, { withCredentials: true })  

3. 서버측 CORS 확인 - ASP.NET WEBAPI

- 설정(가상으로 이름 설정)

  서버 : api.test.com 
  서버 : client.test.com
- ASP.NET WEB API 설정
  /App_Start/WebApiConfig.cs
 
public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    ......

    // Enable CORS for the front-end App
    List<string> orginList = new List<string>() {
      "http://client.test.com:3000","https://client.test.com:3001"
    };
    string strOrgin = string.Join(",", orginList.ToArray());
    var cors = new EnableCorsAttribute(strOrgin, "*", "*") {  SupportsCredentials = true };
    config.EnableCors(cors);
    ......
  }
}