如何在Blazor Web程序集中获取id_token [英] How to get the id_token in blazor web assembly

查看:370
本文介绍了如何在Blazor Web程序集中获取id_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有oidc身份验证的Blazor WebAssembly(最新的3.2.0)应用程序. asp.net身份验证提供了一种获取accessToken的方法,但看不到任何访问id_token(jwt)的方法,这是我的方案所需的. 我可以在浏览器的本地存储中看到id_token. 什么是最好的访问方式?

I have got a Blazor WebAssembly (latest 3.2.0) app with oidc Authentication. The asp.net authentication provides a way to get the accessToken but can't see any means to access the id_token (jwt) which is required for my scenario. I can see the id_token in the local storage of the browser. What would be best way to access it?

谢谢

推荐答案

您可以使用JSInterop从会话存储中读取它,它存储在键 oidc.user:{app baseUri}:{app client id}中:

You can read it from the session storage using JSInterop, it is stored at key oidc.user:{app baseUri}:{app client id} :

@inject IJSRuntime JSRuntime
@inject NavigationManager NavigationManager
...
@code {
     private async Task<string> ReadIdToken()
     {
          const string clientId = "your oidc client id";
          var userDataKey = $"oidc.user:{NavigationManager.BaseUri}:{clientId}";
          var userData = await JSRuntime.InvokeAsync<UserData>("sessionStorage.getItem", userDataKey);
          return userData.id_token;          
     }

     class UserData
     {
         public string id_token { get; set; }
         public int expires_at { get; set; }
     }
}

这篇关于如何在Blazor Web程序集中获取id_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆