简体   繁体   English

如何使用 JavaScript 获取当前登录的用户 ID

[英]How to get the current logged in userId with JavaScript

I want to get the current logged-in userId.我想获取当前登录的用户 ID。 How can I do this with plain JavaScript?我怎么能用普通的 JavaScript 做到这一点?

I can do it with a get request that uses security claims in the backend with c#, but is there a better way?我可以通过使用 c# 在后端使用安全声明的获取请求来做到这一点,但有更好的方法吗?

Depending on the way how login is implemented... Assume that you use API's and JWT.取决于登录的实现方式...假设您使用 API 和 JWT。 So usually when you log in, you get a JWT assigned, it can already hold some of those details (depends on the end-point what you get).所以通常当你登录时,你会得到一个分配的 JWT,它已经可以保存其中的一些细节(取决于你得到的端点)。 Use http://jwt.io and you'll see.使用http://jwt.io你会看到。 There are then tools in the JavaScript world to decode a JWT and get those data.然后在 JavaScript 世界中有工具可以解码 JWT 并获取这些数据。 Anyhow, if you do a single additional call, then cache the result, that is just as fine, IMO.无论如何,如果您进行一次额外的调用,然后缓存结果,那也很好,IMO。 Also you usually store JWT in a localStorage or so, so those data are avail.此外,您通常将 JWT 存储在 localStorage 左右,因此这些数据可用。 all the time...每时每刻...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM