c# - 在 C# 中使用 HttpSelfHostServer 验证客户端证书
问题描述
我有一个用 C# 编写的服务,目前可以在 Http 上运行。我想将此迁移到 Https 并希望当某些客户端使用此服务时,应该有一个选项来选择证书以继续,并且只允许一个特定 CA 颁发的证书。
下面是我所做的代码。
HttpSelfHostServer CreateServer(){
var config = new MySelfHostConfiguration("https://localhost:1234/myService");
IWebApiApplication application = new SelfHostedApplication(config); //this is to do some logging and other configuration.
HttpSelfHostServer selfHost = new HttpSelfHostServer((HttpSelfHostConfiguration)application.HttpConfiguration); //application.HttpConfiguration is same as config
selfHost.OpenAsync.Wait();
return selfHost;
}
public class MySelfHostConfiguration : HttpSelfHostConfiguration{
public MySelfHostConfiguration(string address) : base(address){}
public MySelfHostConfiguration(Uri address) : base(address){}
protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding){
httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
return base.OnConfigureBinding(httpBinding);
}
}
下面是控制器代码
[Authorize]
public class HelloController : ApiController
{
public string Get()
{
//some code to do stuff
}
}
我到目前为止所做的是
- 使用 netsh 和证书指纹将端口 1234 绑定到证书。
有了这个,我可以浏览到https://localhost:1234/myService(即使它现在不安全,因为我使用了测试证书)。但我也希望当我浏览到 URL 时,我应该被要求选择证书。我想我在需要请求证书和验证 CA 的代码中遗漏了一些东西。有人可以建议缺少什么或需要做什么。
PS:我无法从 HttpSelfHostServer 更改为其他任何内容。
解决方案
尝试clientcertnegotiation=enable
与 netsh 一起使用。我希望这个对你有用
推荐阅读
- r - 生成森林图时出错(错误:断言失败。必须满足以下条件之一..)
- bash - 从 Bash 中的字符串末尾删除特定字符的序列
- javascript - 使用java脚本过滤对象的嵌套数组
- javascript - 我怎样才能使这个日期逻辑更通用?每 n [timeunit] 翻转字符串
- php - 如何在队列 Laravel 中运行 Laravel Dusk 测试
- beautifulsoup - bs4 'find() 没有关键字参数' 错误
- python - 嗨,有人可以帮助我。我不知道问题是什么,而且我是编码新手。我正在使用 python 并且我试图创建一个学生类
- jenkins - 重新启动远程桌面后如何保持詹金斯代理在线
- vaadin - Vaadin 数据绑定器 - 组合框问题
- dax - 如何在 DAX Studio 中获取表行数