首页 .NET/Web NETCore之Token信息Claims获取用户登录信息

NETCore之Token信息Claims获取用户登录信息

作者:胡同里的砖头 围观群众:277 更新于:2024-01-30

设置值:

    List<Claim> claims = new List<Claim>()
{
new Claim(ClaimTypes.NameIdentifier, userNo),
new Claim(ClaimTypes.Name, userName),
new Claim("ntAccount", card.ntAccount),
new Claim("userNo", card.userNo),
//new Claim(ClaimTypes.Role, "admin")
};
获取:
 var claims = HttpContext.User.Claims;
//获取用户token
var access_token = HttpContext.GetTokenAsync("access_token");
var accessToken = HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);

var refresh_token = HttpContext.GetTokenAsync("refresh_token");
var refreshToken = HttpContext.GetTokenAsync(OpenIdConnectParameterNames.RefreshToken);

//获取用户信息
var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var username = HttpContext.User.FindFirst(d => d.Type == "preferred_username")?.Value;
var roleName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
var clientId = HttpContext.User.FindFirst(d => d.Type == "client_id")?.Value;

var user_id = HttpContext.User.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name")?.Value;
var role_name = HttpContext.User.FindFirst(d => d.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role")?.Value;

  • 本文标题: NETCore之Token信息Claims获取用户登录信息
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8