设为首页 收藏本站
查看: 1185|回复: 0

[经验分享] Web API 2 使用SSL

[复制链接]

尚未签到

发表于 2017-12-28 22:51:38 | 显示全部楼层 |阅读模式
在Server上启用SSL

稍后我会想在IIS 7 上配置SSL,现在先往下看。
本地测试,您可以启用SSL的IIS Express Visual Studio。在属性窗口中,启用SSL设置为True。注意SSL URL的值,使用这个URL用于测试HTTPS连接。


执行SSL在Web API控制器

如果你有一个HTTPS和HTTP绑定,客户仍然可以使用HTTP访问网站。你可能会允许一些资源可以通过HTTP,而其他资源需要SSL。在这种情况下,使用一个操作过滤器需要SSL对受保护的资源。下面的代码显示了一个Web API检查SSL身份验证过滤器:

C#  

public>
{  public override void OnAuthorization(HttpActionContext actionContext)
  {
  if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
  {
  actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
  {
  ReasonPhrase = "HTTPS Required"
  };
  }
  else
  {
  base.OnAuthorization(actionContext);
  }
  }
  
}
  

  

这个过滤器添加到任何Web API需要SSL的操作:

C#  

public>
{
[RequireHttps]

  public HttpResponseMessage Get() { ... }
  
}
  

  

SSL客户端证书

SSL提供了通过使用公钥基础设施证书身份验证。服务器必须提供一个服务器给客户端进行身份验证的证书。是不太常见的客户端提供一个证书服务器,但这是一个选择的对客户进行身份验证。使用客户端证书的SSL,您需要一种方法来签名证书分发给你的用户。对于许多应用程序类型,这不会是一个很好的用户体验,但在某些环境中(例如,企业)可能是可行的。

优势劣势——证书凭据比用户名/密码。SSL提供了一个完整的安全通道,与认证、消息完整性和消息加密。
——你必须获取和管理PKI证书。——客户机平台必须支持SSL客户端证书。
配置IIS接受客户端证书,打开IIS管理器和执行以下步骤:


  • Click the site node in the tree view.
  • Double-click the SSL Settings feature in the middle pane.
  •   Under Client Certificates, select one of these options:

    • Accept: IIS will accept a certificate from the client, but does not require one.
    • Require: Require a client certificate. (To enable this option, you must also select "Require SSL")

你也可以设置这些选项ApplicationHost.config文件:

xml  

<system.webServer>  <security>
  <access sslFlags="Ssl, SslNegotiateCert" />
  <!-- To require a client cert: -->
  <!-- <access sslFlags="Ssl, SslRequireCert" /> -->
  </security>
  
</system.webServer>
  

  

The SslNegotiateCert flag means IIS will accept a certificate from the client, but does not require one (equivalent to the "Accept" option in IIS Manager). To require a certificate, set the SslRequireCert flag. For testing, you can also set these options in IIS Express, in the local applicationhost.Config file, located in "Documents\IISExpress\config".

为了测试创建一个客户端证书

For testing purposes, you can use MakeCert.exe to create a client certificate. First, create a test root authority:

console  

makecert.exe -n "CN=Development CA" -r -sv TempCA.pvk TempCA.cer  

  

Makecert will prompt you to enter a password for the private key.
Next, add the certificate to the test server's "Trusted Root Certification Authorities" store, as follows:


  • Open MMC.
  • Under File, select Add/Remove Snap-In.
  • Select Computer Account.
  • Select Local computer and complete the wizard.
  • Under the navigation pane, expand the "Trusted Root Certification Authorities" node.
  • On the Action menu, point to All Tasks, and then click Import to start the Certificate Import Wizard.
  • Browse to the certificate file, TempCA.cer.
  • Click Open, then click Next and complete the wizard. (You will be prompted to re-enter the password.)
Now create a client certificate that is signed by the first certificate:

console  

makecert.exe -pe -ss My -sr CurrentUser -a sha1 -sky exchange -n "CN=name"  -eku 1.3.6.1.5.5.7.3.2 -sk SignedByCA -ic TempCA.cer -iv TempCA.pvk
  

  

在Web API中使用客户端证书

On the server side, you can get the client certificate by calling GetClientCertificate on the request message. The method returns null if there is no client certificate. Otherwise, it returns an X509Certificate2 instance. Use this object to get information from the certificate, such as the issuer and subject. Then you can use this information for authentication and/or authorization.

C#  

X509Certificate2 cert = Request.GetClientCertificate();  
string issuer = cert.Issuer;
  
string subject = cert.Subject;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-429141-1-1.html 上篇帖子: IIS8如何安装和使用URL重写工具-URL Rewrite 下篇帖子: ASP.NET机制详细的管道事件流程(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表