小编Nis*_*hah的帖子

如何在asp.net core中从JWT检索ClaimsPrincipal

在我的解决方案中,我有两个项目。1) Web API 和 2) MVC。我正在使用 ASP.NET Core。API 发出 JWT 令牌,MVC 使用它来获取受保护的资源。我正在使用openiddict库来发布 JWT。在 MVC 项目中,在 AccountController Login 方法中,我想检索 ClaimsPrincipal (使用 JwtSecurityTokenHandler ValidateToken 方法)并分配给 HttpContext.User.Claims 和 HttpContext.User.Identity。我想将令牌存储在会话中,并且对于成功登录后的每个请求,将其在标头中传递给 Web API。我可以成功地发出 JWT 并在 MVC 项目中使用它,但是当我尝试检索 ClaimsPrincipal 时,它会抛出一个错误。首先,我什至不确定从 JWT 检索 ClaimsPrinciapal 是否是正确的方法。如果是的话,前进的方向是什么?

WebAPI.启动.CS

public class Startup
{
    public static string SecretKey => "MySecretKey";
    public static SymmetricSecurityKey SigningKey => new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build(); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core openiddict

6
推荐指数
2
解决办法
9803
查看次数

在ASP.Net Core 2.2中,复杂对象以NULL的形式出现在HttpPut中

我正在使用Asp.Net Core 2.2构建一个webapi。我的一个控制器有一个Put方法来更新实体。问题是,我从邮递员休息客户端传递的复杂对象始终为null。

[HttpPut]
[Route("{searchPatternId:long}")]
public async Task<IActionResult> Put(long searchPatternId, [FromBody]SearchPattern searchPattern)
{
        try
        {
            if (searchPattern == null) return BadRequest();

            return Ok(await _searchPatternService.PutAsync(searchPattern));
        }
        catch (Exception e)
        {
            await _errorLogService.Log(e);
            return StatusCode(500);
        }
}
Run Code Online (Sandbox Code Playgroud)

SearchPattern类:

[Table("search_pattern", Schema = "abc")]
public class SearchPattern
{
    [Key]
    public long SearchPatternId { get; set; }
    public string Pattern { get; set; }
    public long PatternHash { get; set; }
    public int? Age { get; set; }
    public string Location { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

0
推荐指数
1
解决办法
70
查看次数

标签 统计

asp.net-core ×2

c# ×2

asp.net-core-mvc ×1

openiddict ×1