我正在尝试从令牌访问用户 ID,但我尝试的所有内容都返回 null。生成的令牌具有必要的信息,所以我认为它不是令牌生成。
这是创建令牌的部分
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_jwtSettings.Secret);
var tokenDescriptor = new SecurityTokenDescriptor()
{
Subject = new ClaimsIdentity(new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.Email),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(ClaimTypes.NameIdentifier, existingAppUser.Id),
new Claim("id", existingAppUser.Id),
}),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return new AuthenticationResult()
{
Token = tokenHandler.WriteToken(token)
};
Run Code Online (Sandbox Code Playgroud)
当我解码生成的令牌时,我可以看到令牌中的所有声明,但我无法在项目中访问它。
这是尝试访问名称标识符或 id 声明的部分
var claimsList = _httpContextAccessor.HttpContext.User.Claims.ToList();
var identityName = _httpContextAccessor.HttpContext.User.Identity.Name;
var nameId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var id …Run Code Online (Sandbox Code Playgroud) 我有一个包含多个项目的解决方案。其中之一用于仪表板 Web 应用程序,其中之一用于 API 项目。是否可以在同一个程序集中运行这两个应用程序?
例如,Web 应用程序将在“localhost:5001”上运行,API 项目将在“localhost:5001/api/”上运行。如果可能的话,它们会共享相同的内存缓存管理器吗?