# How to use Okta to authenticate Blazor WASM Asp.NET hosted app?

**URL:** <https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345>\
**Category:** OAuth/OIDC\
**Created:** [July 24, 2022, 3:16am UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345 "2022-07-24T03:16:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rechokta](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rechokta](https://devforum.okta.com/u/rechokta)\
**Post date:** [July 24, 2022, 3:16am UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345/1 "2022-07-24T03:16:31Z")

</div>

we have sample on how to authenticate Blazor Server app ([Authentication and Authorization with Okta in .NET 6 Blazor Server - YouTube](https://www.youtube.com/watch?v=MvKK0UlgxGI)) and Blazor WASM PWA app ([How to Build Securely with Blazor WebAssembly (WASM) | Okta Developer](https://developer.okta.com/blog/2020/09/30/blazor-webassembly-wasm-dotnetcore))

Does anyone have a sample or know how to use Okta to secure Blazor WASM [Asp.net](http://Asp.net) core hosted app?  
this template produces 3 projects (Client/Server/Shared)?

![image](https://global.discourse-cdn.com/oktadev/original/2X/a/a1f92d13048fca77acc59edea630544a2e4d9284.png)

![image](https://global.discourse-cdn.com/oktadev/original/2X/9/9de9f9b75a457a5f40737018099121b1cd27910f.png)

Thanks very much!  
John

---

<div class="post-metadata">

**Author:** ![rechokta](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rechokta](https://devforum.okta.com/u/rechokta)\
**Post date:** [July 25, 2022, 3:51am UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345/2 "2022-07-25T03:51:20Z")

</div>

I managed to get the client authentication work by following this link [How To Add Okta Authentication to Blazor WASM - YouTube](https://www.youtube.com/watch?v=fQSE0lptDfA&t=162s) but the [authorize] does not work.  
for .NET 6, you need to add Microsoft.IdentityModel.Tokens nuget package to server project get it working.  
I checked that the token is stored in sessionstorage:  
oidc.user:{{your\_okta\_domain}}/oauth2/default:0oajhf0akzw1YfG1w0h7 contains  
access\_token: “”  
expires\_at: 1658723964  
id\_token: “”  
profile: {sub: “00u17vftfpmmUi3lr0h8”, name: “”, ver: 1,…}  
scope: “openid profile”  
token\_type: “Bearer”

I also verified that the bearer token is attached to Blazor client Http request due to these line in Program.cs (Client project):

builder.Services.AddHttpClient(“ServerAPI”, client =\> client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))  
.AddHttpMessageHandler();

// Supply HttpClient instances that include access tokens when making requests to the server project  
builder.Services.AddScoped(sp =\> sp.GetRequiredService().CreateClient(“ServerAPI”));

but at server project, if you put [Authorize] on the controller, you will get 401 unauthorized, I checked the the User context is not constructed in server project. but the bearer token is passed … I followed this link [Secure Your .NET 6 Web API | Okta Developer](https://developer.okta.com/blog/2022/04/20/dotnet-6-web-api), I was able to validate the token.

I still need to find a solution on following:

1. how to make server project [Authorize] working
2. how to make my role, which is group membership appear in the token as claim

Thank you for your attention!

---

<div class="post-metadata">

**Author:** ![rechokta](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rechokta](https://devforum.okta.com/u/rechokta)\
**Post date:** [July 27, 2022, 4:31pm UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345/3 "2022-07-27T16:31:22Z")

</div>

I was able to create a groups claim in the authorization server and now the groups claim is included in the access token and id\_token. but this custom claim is not populated in the signedin user’s claims. I am trying to find a way to intercept the signin event to populate that information - no luck yet.

found another interesting issue:  
we have another api who support the okta token - sso, if I use the following process to get a token it works fine, but the access token I got from Blazor client sign in will not work for that API - does anyone know why?  
the steps to get access token works:

1. get a ssoSessionToken by passing username and password to authn endpoint
2. use this ssoSessionToken to get id token from /oauth2/v1/authorize

what is the difference between the token acquired this way and the token from okta hosted widget?

---

<div class="post-metadata">

**Author:** ![rechokta](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rechokta](https://devforum.okta.com/u/rechokta)\
**Post date:** [August 2, 2022, 6:08am UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345/4 "2022-08-02T06:08:50Z")

</div>

got all working … [GitHub - pursca/BlazorWasmHosted-Okta: Sample code to use Okta as identity server to authenticate Blazor WebAsembly Asp.net core hosted app and secure the server API](https://github.com/pursca/BlazorWasmHosted-Okta)

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 12, 2024, 9:27pm UTC](https://devforum.okta.com/t/how-to-use-okta-to-authenticate-blazor-wasm-asp-net-hosted-app/21345/5 "2024-02-12T21:27:15Z")

</div>

Closed on February 12, 2024, 9:27pm UTC


