Is it possible to convert this JS code into .NET?


Joined
Oct 19, 2022
Messages
1
Reaction score
0
I really need help and i would apreciate if someone can help me with this I need to do the same than here but with .NET, instead of node js. I never worked with .NET before.

Is my first time working with tokens and APIs. So i kinda need help with this

Question: Is it possible to do this with .NET? if it's not possible, i would like to know it. thanks.

UH API Behavior - Error handling

. Sample API request with retry for refreshing the token (if the app is using axios and MSAL.js).

Code:
get: async (url, retry = true) => {

        const _url = url;

        const headers = {

            Authorization: `Bearer ${sessionStorage.getItem("user_token")}`

        };

        const _retry = retry;

        const axiosInstance = axios.create({

            headers,

            responseType: "application/json"

        });

        axiosInstance.interceptors.response.use(

            (res) => {                return res;

            },

            async (error) => {

                // debugger;

                const status = error.response ? error.response.status : null;

                if (status === 401 && _retry) {

                    sessionStorage.removeItem("user_token");

              const hasToken = await  refreshToken();                 

                        return httpClirefreshTokenentServiceWithRefreshToken.get(

                            _url,

                            false

                        );                   

                }

                return Promise.reject(error);

            }

        );

        return new Promise((resolve, reject) => {

            axiosInstance

                .get(_url)

                // .post(url, data, headers)

                .then((response) => {

                    if (response.status === 200) {

                        resolve(response.data);

                    } else {

                        resolve(response);

                    }

                })

                .catch((e) => {

                    reject(e);

                });

        });

    },
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top