asp.net core webapi 获取请求头token

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
 
[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
    [HttpGet]
    public IActionResult Get()
    {
        // 获取单个请求头
        StringValues headerValue = HttpContext.Request.Headers["HeaderName"]; // 替换"HeaderName"为你想获取的头名称
 
        // 获取所有请求头
        IHeaderDictionary headers = HttpContext.Request.Headers;
 
        // 将请求头存入字典
        Dictionary<string, string> headersDict = new Dictionary<string, string>();
        foreach (var header in headers)
        {
            headersDict[header.Key] = header.Value;
        }
 
        // 根据需要处理headerValue, headers, 或 headersDict
        // ...
 
        return Ok(); // 返回你的响应
    }
}

 

posted @ 2024-09-24 13:38  龙卷风吹毁停车场  阅读(174)  评论(0)    收藏  举报