小编Alp*_*ğan的帖子

JWT Token 访问 AuthenticatedUser

我正在尝试从令牌访问用户 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)

c# asp.net jwt asp.net-core asp.net-core-3.1

8
推荐指数
1
解决办法
180
查看次数

.NET 5 同一程序集中的多个项目

我有一个包含多个项目的解决方案。其中之一用于仪表板 Web 应用程序,其中之一用于 API 项目。是否可以在同一个程序集中运行这两个应用程序?

例如,Web 应用程序将在“localhost:5001”上运行,API 项目将在“localhost:5001/api/”上运行。如果可能的话,它们会共享相同的内存缓存管理器吗?

asp.net-mvc asp.net-web-api .net-5

3
推荐指数
1
解决办法
756
查看次数