0

I need to authenticate the user on the backend using the authentication cookie I have stored. I need to request API in Vue using axios requests and send cookie in those requests. The user should then be authenticated in the backend controller. I store the cookie as a whole:

public string GetOrchardAuthCookie()
{
    return new ChunkingCookieManager().GetRequestCookie(_httpContextAccessor.HttpContext, "orchauth_Default");
}

my axios request:

axios.get(apiUrl, {
        headers: {
            "orchauth_Default": mySavedCookie
        }
    })
        .then(response =>
        {
            this.categories = response;
        })
        .catch(this.errorHandle);

after the request is made and I stop the code in the controller, the user has 0 claims

How to make Axios request to authenticate User in asp .net core controller?

Foro
  • 35
  • 10
  • Could this case answer your question?https://stackoverflow.com/questions/52549079/does-axios-support-set-cookie-is-it-possible-to-authenticate-through-axios-http – Ruikai Feng May 16 '22 at 08:11
  • @RuikaiFeng I have my Orchauth_Default cookie saved in html input element. I need to retrieve it inside widget and send it in api requests. My backend api then should automatically autenticate user. withCredentials: true has nothing to do with this, you cannot add cookie with this solution. Maybe i just do not understand correctly, feel free to explain. :) – Foro May 19 '22 at 07:16

1 Answers1

0

I found a solution. The cookie had to be passed to Axios as follows:

axios.defaults.headers.common['orchauth_Default'] = mySavedCookie;

my OrchardCore backend will now automatically authenticate users in the api controller

Foro
  • 35
  • 10