当前位置: 首页 > news >正文

ASP .Net Core创建一个httppost请求并添加证书

ASP .Net Core创建一个httppost请求并添加证书

创建.net Core程序,使用自签名证书,可以处理https的get和post请求。

创建证书

创建自签名证书的流程可以在这里查看:

https://blog.csdn.net/GoodCooking/article/details/139815278

创建完毕后:
在这里插入图片描述

继续输入命令,创建.pfx 证书,

openssl pkcs12 -export -out myNameZhengShu\cert.pfx -inkey myNameZhengShu\key.pem -in myNameZhengShu\cert.pem

在这里插入图片描述输入密码123456,当然是看不到的啦

在这里插入图片描述在这里插入图片描述

一共是输入三次123456
最后生成cert.pfx 文件
在这里插入图片描述

配置.net Core

将证书放到.netCore的程序路径中
在这里插入图片描述

修改.netCore的程序的Program.cs 文件的内容

using System.Security.Cryptography.X509Certificates;var builder = WebApplication.CreateBuilder(args);// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();var app = builder.Build();// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{app.UseSwagger();app.UseSwaggerUI();
}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();// 配置 Kestrel 使用 SSL 证书
app.UseHttpsRedirection();
app.Use(async (context, next) =>
{var certificate = new X509Certificate2(Path.Combine(AppContext.BaseDirectory, "myNameZhengShu", "cert.pfx"),"123456");context.Connection.ClientCertificate = certificate;await next();
});app.Run();

重要的是这个,myNameZhengShu 是证书的路径,我这里是.exe程序的myNameZhengShu 的文件夹下,证书的密码是123456,证书的名称是cert.pfx

app.UseHttpsRedirection();
app.Use(async (context, next) =>
{var certificate = new X509Certificate2(Path.Combine(AppContext.BaseDirectory, "myNameZhengShu", "cert.pfx"),"123456");context.Connection.ClientCertificate = certificate;await next();
});

然后添加一个http的post请求,创建一个新的.cs文件并拷贝粘贴下面的内容:
访问方式是:https://localhost:7267/User?username=123
请求结果是:123, 你好,现在是:2024-06-20 22:46:01

using Microsoft.AspNetCore.Mvc;
namespace TSLServerTest.Controllers
{[ApiController][Route("[controller]")]public class UserController : ControllerBase{// POST请求的示例[HttpPost]public ActionResult<string> Post([FromQuery] string username){// 获取当前时间DateTime currentTime = DateTime.Now;// 构建返回的字符串string responseMessage = $"{username}, 你好,现在是:{currentTime.ToString("yyyy-MM-dd HH:mm:ss")}";return Ok(responseMessage);}}
}

验证证书生效

在这里插入图片描述

在post man中发送http请求发送不了

在这里插入图片描述https就可以哦
在这里插入图片描述

http://www.lryc.cn/news/378867.html

相关文章:

  • Redis入门篇
  • 变电站智能巡检机器人解决方案
  • Linux Kernel入门到精通系列讲解(QEMU-虚拟化篇) 2.5 Qemu实现RTC设备
  • 【自动驾驶】通过下位机发送的加速度、角速度计算机器人在世界坐标系中的姿态
  • Python 设计模式(第2版) -- 第四部分(其他设计模式)
  • gitlab升级16.11.3-ee
  • 剑指offer 算法题(搜索二维矩阵)
  • SaaS平台数据对接为什么要选择API对接?
  • 力扣136. 只出现一次的数字
  • 重学java 74.Lombok的使用
  • 数据结构6---树
  • 一键制作,打造高质量的数字刊物
  • Java面试题:对比继承Thread类和实现Runnable接口两种创建线程的方法,以及它们的优缺点
  • 编译原理-各章典型题型+思路求解
  • 【绝对有用】C++ vector排序
  • linux——VScode安装
  • X-LoRA:高效微调 LoRA 系列,实现不同领域知识专家混合模型
  • 基于卷积神经网络的目标检测
  • Mysqld数据库管理
  • Wifi通信协议:WEP,WPA,WPA2,WPA3,WPS
  • 开源【汇总】
  • 英文字母表
  • Redis缓存穿透
  • SHELL脚本学习(十一)正则表达式
  • Leetcode Java学习记录——代码随想录哈希表篇
  • 我又挖到宝了!小米、352、希喂宠物空气净化器除毛能力PK
  • 每月 GitHub 探索|10 款引领科技趋势的开源项目
  • 【如何让新增的Android.mk参与编译】
  • 【windows|009】计算机网络基础知识
  • C语言循环中获取之前变量的值