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).

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);
                });
        });
    },

Recommended Answers

All 3 Replies

I won't write code for this for a few reasons. The first is .NET doesn't reveal the language we would be using and second, I can't divine what the app in question is intended to do. Big picture matters so if I was doing research about tokens, refresh and my choice would be C# but I can't tell what the app's intent is, even still I'd read https://auth0.com/blog/use-refresh-tokens-in-aspnet-core-apps/ to see if there's clues to be had for ASP.NET.

I can't divine what the app in question is intended to do

It seems obvious to me they are looking for a .net solution to working with an OAuth-based API. My guess is that they have a .net app that they are trying to add OAuth to with no prior .net experience, and don't even know/realize that there are multiple .net languages.

commented: There's C#, ASP, Visual Basic and what else? Queue 3 blind men and the elephant. +16

As a completely separate thing, DaniWeb, as you may know, already has its own OAuth-based API where we have a tutorial/documentation about access tokens and refresh tokens. It's targeted to the PHP developer (just because that's my personal language of choice), but if you don't have a lot of prior API experience, it will help you to understand how access and refresh tokens work (which is exactly the same across all languages).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.