HttpSelfHostConfiguration可以開啟自宿主服務(wù),即監(jiān)聽本地端口,然后發(fā)布對應(yīng)的api服務(wù),其實(shí)就是個(gè)webapi項(xiàng)目,但是可以不用掛載在IIS上,程序直接運(yùn)行就可以
上次做完了HttpSelfHostConfiguration后,有要求,需要讓自宿主服務(wù)支持https協(xié)議,這就麻煩了
要知道https可是需要證書啥的,剛好研究了一段時(shí)間,再同事的幫助下也算是做出來了,下來貼點(diǎn)教程數(shù)據(jù)上來
整個(gè)注冊流程中需要用到IIS來生成證書
- 注冊安全證書
在IIS中選擇服務(wù)器證書
依次點(diǎn)擊 創(chuàng)建自簽名證書->輸入名稱->確定保存
拷貝證書的指紋
點(diǎn)擊新增的證書信息,拷貝指紋數(shù)據(jù),用以備用 - 使用cmd命令 刪除、注冊證書
使用https需要 要用CMD命令 將證書的指紋和 系統(tǒng)的端口、軟件的GUID進(jìn)行綁定
這樣子 HttpSelfHostConfiguration才能啟動(dòng)對應(yīng)端口的https服務(wù),當(dāng)然證書肯定也是用的 咱們新創(chuàng)建的證書嘍,這一步 我略去,僅把指令貼出來,因?yàn)槲液竺鏁么a去執(zhí)行 這個(gè)cmd命令
先執(zhí)行刪除,在執(zhí)行添加注冊,防止端口已經(jīng)被其他證書占用了
// port=端口,pfx=證書指紋 ,guid=當(dāng)前程序GUID
netsh http delete sslcert ipport=0.0.0.0:port
netsh http add sslcert ipport=0.0.0.0:port certhash=pfx appid={Guid}
- 代碼中啟動(dòng) https服務(wù)
using Newtonsoft.Json;
using Robot2.OpenApi.V2.Filter;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;
using System.Web.Http.SelfHost.Channels;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
try
{
string url = string.Format("https://localhost:20101");
string port = "20101";
string pfx = "e063bb5f4578a211fab76439e38cc93d92e56286";
string guid = "78a9b36e-51e3-44e9-9ee0-abfd6d0e6b8d";
List<string> cmds = new List<string>() {
$"/c netsh http delete sslcert ipport=0.0.0.0:{port}",
$"/c netsh http add sslcert ipport=0.0.0.0:{port} certhash={pfx} appid="+"{"+guid+"}",
};
foreach (string cmd in cmds)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = cmd,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = false
}
};
process.Start();
process.WaitForExit();
}
//var config = new HttpSelfHostConfiguration(url);
var config = new ExtendedHttpSelfHostConfiguration(url);//使用Https
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
config.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
HttpSelfHostServer _hostServer = new HttpSelfHostServer(config);
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, PUT, DELETE, OPTIONS") { SupportsCredentials = true });
config.Filters.Add(new AuthorFilter());
config.Filters.Add(new ExceptionFilter());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new
{
controller = "Home",
action = "Index",
id = RouteParameter.Optional
});
_hostServer.OpenAsync().Wait();
Console.WriteLine("服務(wù)端口打開成功");
}
catch (Exception ex)
{
Console.WriteLine("服務(wù)端口打開異常:" + JsonConvert.SerializeObject(ex));
}
Console.ReadKey();
}
public class ExtendedHttpSelfHostConfiguration : HttpSelfHostConfiguration
{
public ExtendedHttpSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
public ExtendedHttpSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }
protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
{
if (BaseAddress.ToString().ToLower().Contains("https://"))
{
httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
}
return base.OnConfigureBinding(httpBinding);
}
}
}
}
執(zhí)行https服務(wù)器下的controller方法成功(具體我這個(gè)Controller怎么寫的,大家可以參考我上一篇的
HttpSelfHostConfiguration文章
特別提醒,https和http請求本身存在證書校驗(yàn)的問題,我用的postman請求的數(shù)據(jù),postman默認(rèn)是不支持自簽名證書的,需要設(shè)置下 ,設(shè)置的鏈接如下
https://blog.csdn.net/q7w8e9r4/article/details/132140205文章來源:http://www.zghlxwxcb.cn/news/detail-775066.html
遇到 “postman Self-signed SSL certificate blocked” 錯(cuò)誤是因?yàn)?Postman 檢測到你正在嘗試訪問一個(gè)使用自簽名 SSL 證書的網(wǎng)站,并且默認(rèn)情況下,Postman 會阻止對這樣的網(wǎng)站進(jìn)行請求。 要解決這個(gè)問題,你可以按照以下步驟進(jìn)行操作:
1.打開 Postman:打開 Postman 應(yīng)用程序。
2.設(shè)置 SSL 證書驗(yàn)證:點(diǎn)擊左上角的 “Settings”(設(shè)置)按鈕,然后選擇 “Settings”(設(shè)置)選項(xiàng)。
3.禁用 SSL 證書驗(yàn)證:在 “Settings”(設(shè)置)頁面的左側(cè)導(dǎo)航欄中,選擇 “General”(常規(guī))選項(xiàng)。在右側(cè)的 “SSL certificate verification”(SSL 證書驗(yàn)證)部分,將開關(guān)切換為關(guān)閉狀態(tài)。
4.重新發(fā)送請求:回到 Postman 的主界面,重新發(fā)送你的請求。 請注意,禁用 SSL 證書驗(yàn)證可能會導(dǎo)致你的請求不再驗(yàn)證服務(wù)器的證書,從而可能存在安全風(fēng)險(xiǎn)。因此,請確保你對要訪問的網(wǎng)站的可信度有所了解,并仔細(xì)考慮在禁用 SSL 證書驗(yàn)證之前的安全性。 如果你仍然希望進(jìn)行 SSL 證書驗(yàn)證,但遇到了自簽名 SSL 證書的問題,你可以考慮導(dǎo)入服務(wù)器的自簽名證書到 Postman 中。具體步驟如下:
5.獲取自簽名 SSL 證書:從服務(wù)器管理員或網(wǎng)站所有者獲取自簽名 SSL 證書文件(通常是以 .crt 或 .pem 結(jié)尾的文件)。
6.導(dǎo)入 SSL 證書:在 Postman 應(yīng)用程序中,點(diǎn)擊左上角的 “Settings”(設(shè)置)按鈕,然后選擇 “Settings”(設(shè)置)選項(xiàng)。在 “Settings”(設(shè)置)頁面的左側(cè)導(dǎo)航欄中,選擇 “Certificates”(證書)選項(xiàng)。點(diǎn)擊 “Add Certificate”(添加證書)按鈕,并選擇你的 SSL 證書文件。按照指示完成證書導(dǎo)入過程。
7.重新發(fā)送請求:回到 Postman 的主界面,重新發(fā)送你的請求。 通過導(dǎo)入自簽名 SSL 證書,Postman 將能夠驗(yàn)證服務(wù)器的證書,并安全地進(jìn)行請求。
————————————————
版權(quán)聲明:本文為CSDN博主「抓飯不吃皮牙子」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/q7w8e9r4/article/details/132140205文章來源地址http://www.zghlxwxcb.cn/news/detail-775066.html
到了這里,關(guān)于HttpSelfHostConfiguration+.net framework4.5.2 發(fā)布HTTPS服務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!