# Using react front end with .net backend token is rejected

**URL:** <https://community.auth0.com/t/using-react-front-end-with-net-backend-token-is-rejected/118416>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [October 12, 2023, 3:26pm UTC](https://community.auth0.com/t/using-react-front-end-with-net-backend-token-is-rejected/118416 "2023-10-12T15:26:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjones](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@bjones](https://community.auth0.com/u/bjones)\
**Post date:** [October 12, 2023, 3:26pm UTC](https://community.auth0.com/t/using-react-front-end-with-net-backend-token-is-rejected/118416/1 "2023-10-12T15:26:21Z")

</div>

We’ve been trying to get this to work for a while now and I’m at wits end.

We have a react front end using the react SDK. In auth0 we have an application for Single Page Application that the front end can authenticate with and pull profile data from auth0. We have another application that is Machine to Machine that the backend is connected with.

We can use insomnia to get a token and access the backend however the fronted is rejected 401

Here is the relevant react code

```auto
import { createContext, useState, useEffect } from 'react';
import { useAuth0, Auth0Provider } from '@auth0/auth0-react';

export const AuthContext = createContext();

export const TokenProvider = ({ children }) => {
    const [token, setToken] = useState(null);
    const { isAuthenticated, getAccessTokenSilently } = useAuth0();

    useEffect(() => {
        if (isAuthenticated) {
            getAccessTokenSilently()
                .then(token => setToken(token))
                .catch(err => console.log('Error fetching token:', err));
        }
    }, [isAuthenticated, getAccessTokenSilently]);

    return (
        <AuthContext.Provider value={{ token }}>
            {children}
        </AuthContext.Provider>
    );
}

export const AuthProvider = ({ children, domain, clientId, authorizationParams }) => {
    return (
        <Auth0Provider
            domain={domain}
            clientId={clientId}
            authorizationParams={authorizationParams}
        >
            <TokenProvider>
                {children}
            </TokenProvider>
        </Auth0Provider>
    );
}

```

And the .net code

```auto
            var domain = $"https://{Configuration["Auth0:Domain"]}/";
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Authority = domain;
                    options.Audience = Configuration["Auth0:Audience"];
                });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("read:messages", policy => policy.Requirements.Add(new HasScopeRequirement("read:messages", domain)));
            });

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 17, 2023, 9:01pm UTC](https://community.auth0.com/t/using-react-front-end-with-net-backend-token-is-rejected/118416/3 "2023-10-17T21:01:33Z")

</div>

Hi @bjones,

Welcome to the Auth0 Community!

There should be an error message telling a bit more about why the token was not accepted. Additionally, can you please post an example token payload (sensitive data excluded). You can decode it using [JWT.io](http://JWT.io).

Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 31, 2023, 9:01pm UTC](https://community.auth0.com/t/using-react-front-end-with-net-backend-token-is-rejected/118416/4 "2023-10-31T21:01:56Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
