# Jwt validation in C#

**URL:** <https://community.auth0.com/t/jwt-validation-in-c/61697>\
**Category:** Get Help\
**Created:** [April 25, 2021, 11:32am UTC](https://community.auth0.com/t/jwt-validation-in-c/61697 "2021-04-25T11:32:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![naama](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/naama/32/13118_2.png) [@naama](https://community.auth0.com/u/naama)\
**Post date:** [April 25, 2021, 11:32am UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/1 "2021-04-25T11:32:36Z")

</div>

I’m trying to validate auth0 JWT in my .net framework web api‏

and have no luck by now‏

my JWT is created by auth0 since I’m using facebook/linkedin/etc verification‏

and posting that token in authorization header under 'bearer’‏

in all of my requests to api‏

in my api i have this class:‏

```
    public class ScopeAuthorizeAttribute : AuthorizeAttribute

```

{  
private readonly string scope;

```
public ScopeAuthorizeAttribute(string scope)
{
    this.scope = scope;
}

public override void OnAuthorization(HttpActionContext actionContext)
{
    base.OnAuthorization(actionContext);

    // Get the Auth0 domain, in order to validate the issuer
    var domain = $"https://{ConfigurationManager.AppSettings["Auth0Domain"]}/";

    // Get the claim principal
    ClaimsPrincipal principal = actionContext.ControllerContext.RequestContext.Principal as ClaimsPrincipal;

    // Get the scope clain. Ensure that the issuer is for the correcr Auth0 domain
    var scopeClaim = principal?.Claims.FirstOrDefault(c => c.Type == "scope" || c.Issuer == domain);
    if (scopeClaim != null)
    {
        // Split scopes
        var scopes = scopeClaim.Value.Split(' ');

        // Succeed if the scope array contains the required scope
        if (scopes.Any(s => s == scope))
            return;
    }

    HandleUnauthorizedRequest(actionContext);
}

```

}

but i’m getting null in this line:  
ClaimsPrincipal principal = actionContext.ControllerContext.RequestContext.Principal as  
ClaimsPrincipal;

what am i doing wrong?

---

<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:** [April 26, 2021, 6:52pm UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/3 "2021-04-26T18:52:48Z")

</div>

Hi @naama,

Have you looked at our quickstart? There is a section that shows how to validate an access token.

> **[Auth0 ASP.NET Core Web API SDK Quickstarts: Authorization](https://auth0.com/docs/quickstart/backend/aspnet-core-webapi/01-authorization#validate-access-tokens)**
>
> This tutorial demonstrates how to add authorization to an ASP.NET Core Web API application using the standard JWT middleware.

I’m not an expert with .NET, but it looks like you might be missing the audience. Have you looked at your token to confirm it is a JWT that can be decoded by your API?

---

<div class="post-metadata">

**Author:** ![naama](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/naama/32/13118_2.png) [@naama](https://community.auth0.com/u/naama)\
**Post date:** [April 27, 2021, 9:17am UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/4 "2021-04-27T09:17:31Z")

</div>

hi, thanks for the quick answer,  
I have an angular application and .net framework web api.  
I worked according to [this](https://auth0.com/docs/quickstart/spa/angular?_ga=2.241595457.255097602.1619418153-1929892949.1614597914)for the angular and [this](https://auth0.com/docs/quickstart/backend/webapi-owin?_ga=2.249912773.255097602.1619418153-1929892949.1614597914) for the web api.  
but no success.

I think my interceptor might not be correct  
what am I missing to make it work?

my interceptor:

```auto
intercept(req: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>>{
const token = this.storageService.get('accessToken');
req = req.clone({
 url: req.url,
 setHeaders: {
Authorization: `Bearer ${token}`
 }
});
return next.handle(req);
 }

```

accessToken:

```auto
auth.getAccessTokenSilently().subscribe(res => {
          this.storageService.set('accessToken', res);
         
        })

```

---

<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:** [April 27, 2021, 10:54pm UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/5 "2021-04-27T22:54:36Z")

</div>

Have you set the `audience` requested by your angular app as the _API identifier_ for your .net API?

` audience: '{YOUR_API_IDENTIFIER}',`

---

<div class="post-metadata">

**Author:** ![naama](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/naama/32/13118_2.png) [@naama](https://community.auth0.com/u/naama)\
**Post date:** [April 28, 2021, 7:28am UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/6 "2021-04-28T07:28:16Z")

</div>

I think I did, maybe I did it wrong though  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/3/9/394bd9ba3e7dd54dc90f18190347f7b8a2b28394.png)

---

<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:** [April 28, 2021, 3:41pm UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/7 "2021-04-28T15:41:33Z")

</div>

Does the request to your .net API include the token? And have you decoded the token (you can use [jwt.io](http://jwt.io)) and confirmed it includes the correct scopes and audience?

---

<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:** [June 1, 2021, 5:07pm UTC](https://community.auth0.com/t/jwt-validation-in-c/61697/8 "2021-06-01T17:07:12Z")

</div>

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