You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.AspNetCore.Mvc;
namespace HttpClientStudy.WebApp.Controllers
{
/// <summary>
/// 普通(简单) 控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class CookieController : ControllerBase
{
private ILogger<CookieController> _logger;
private AccountService _accountService;
/// <summary>
/// 构造
/// </summary>
public CookieController(ILogger<CookieController> logger, AccountService accountService)
{
_logger = logger;
_accountService = accountService;
}
/// <summary>
/// 获取请求中的Cookie
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult GetRequestCookie()
{
var cookies = Request.Cookies;
if (cookies == null || cookies.Count == 0)
{
return Ok(BaseResultUtil.Success("", "没有Cookie"));
}
var result = BaseResultUtil.Success(cookies);
return Ok(result);
}
/// <summary>
/// 设置响应中的Cookie
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult GetResponseCookie()
{
//Cookie选项
var cookieOptions = new CookieOptions
{
// 设置过期时间如1天后过期
Expires = DateTimeOffset.Now.AddDays(1),
// 设置Cookie路径
Path = "/",
// 设置HttpOnly防止XSS攻击
HttpOnly = false,
// 设置Secure仅HTTPS传输
Secure = false,
// 设置SameSite策略
SameSite = SameSiteMode.Unspecified
};
var cookieData = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("ProjectName","WebApp"),
new KeyValuePair<string, string>("Version","Dotnet9"),
};
HttpContext.Response.Cookies.Append(cookieData, cookieOptions);
var result = BaseResultUtil.Success("响应头中已设置Cookie");
return Ok(result);
}
/// <summary>
/// 设置Cookie
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult SetResponseCookie(string cookieName, string cookieValue)
{
Response.Headers.TryAdd("Cookie", $"{cookieName}={cookieValue ?? string.Empty}");
var result = BaseResultUtil.Success($"响应头{cookieName}中已设置Cookie值{cookieValue}");
return Ok(result);
}
/// <summary>
/// 检测Cookie
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CheckCookie(string? cookieName)
{
var cookies = Request.Cookies;
if (cookies == null || cookies.Count == 0)
{
return Ok(BaseResultUtil.Success("", "没有Cookie"));
}
if (string.IsNullOrWhiteSpace(cookieName))
{
var allCookie = BaseResultUtil.Success(cookies, "返回所有Cookie");
return Ok(allCookie);
}
KeyValuePair<string, string> findCookie = cookies.FirstOrDefault(c => c.Key == cookieName);
if (findCookie.Key == null)
{
return Ok(BaseResultUtil.Success($"请求头中没有找到名为{cookieName}的Cookie", $"没找到Cookie"));
}
else
{
return Ok(BaseResultUtil.Success($"请求头中名为{findCookie.Key}的Cookie,其值为{findCookie.Value}", $"找到Cookie"));
}
}
}
}