Home > Mobile >  Handling requests through an external web Page
Handling requests through an external web Page

Time:03-30

I'm building a web app using MVC .NET6. after logging in (to my app) i need to call a rest api but this rest api is using Oauth2 so basically my programm calls a certain web page belongs to the company that made the api were i need to inter my authentication user name and password to be able to get the token and refreshToken. The problem is i don't know how to handle the requests on that page. There's an example provided by the company where it works using axios and javascript but i wanna do it using .net6 if anyone could tell me how to do it using c#?

Thank You very much

With this javascript code using axios server i can get the tokens and access the rest api but i need to do it with c#/.net6 so please help

const server = http.createServer(handleRequest);

server.listen(config.port, () => {
    console.log('Server listening on: ``http://localhost``:%s', config.port);
});

const handleRequest = async (request, response) => {
const requestUrl = url.parse(request.url);

    if (requestUrl.pathname !== '/oauth') {
        response.end();
        return;
    }
    
    const queryParameter = querystring.parse(requestUrl.query);
    const authorizationCode = queryParameter.code;
    const receivedState = queryParameter.state;
    
    if (receivedState !== sessionState) {
        console.log('State in the callback does not match the state in the original request.');
    
        response.end();
        return;
    }
    
    // Get access token
    console.log('Getting tokens...');
    const tokens = await retrieveTokens(authorizationCode);
    console.log('Received new tokens: \n', tokens);
    
    // Get user information
    console.log('Getting user information...');
    const userInformation = await userInfo(tokens.accessToken);
    console.log(userInformation);
    
    // Refresh tokens
    console.log('Refreshing tokens...');
    const refreshedTokens = await refreshTokens(tokens.refreshToken);
    console.log('Received new tokens: \n', tokens);
    
    // Get user information using the refreshed accessToken
    console.log('Getting user information...');
    const userInformationWithRefreshedToken = await userInfo(refreshedTokens.accessToken);
    console.log(userInformationWithRefreshedToken);
    
    response.end();

};

const retrieveTokens = async authorizationCode => {
const requestBody = {
  client_id: config.clientId,
  client_secret: config.clientSecret,
  redirect_uri: config.redirectUri,
  code: authorizationCode,
  grant_type: 'authorization_code',
};

  const response = await axios.post(config.tokenUrl, querystring.stringify(requestBody), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    });
    
    return {
        accessToken: response.data.access_token,
        refreshToken: response.data.refresh_token,
    };

};

CodePudding user response:

The code you provided is just a snippet. Usually OAuth2.0 is implemented in .Net Core, and we will use identityserver4 or okta to implement it.

This part is very complicated, we can't tell you the specific content in the answer, I found a good blog for you.

IdentityServer4, ASP.NET Core API and a client with username/password

  • Related