简体   繁体   English

wcf webservice和使用https进行URL转发

[英]wcf webservice and URL forwarding with https

I have been researching and trying to solve this issue for few days without success. 我一直在研究并尝试解决这个问题几天没有成功。

I have a WCF webservice deployed on IIS 8 on windows server 2012. 我在Windows Server 2012上的IIS 8上部署了WCF Web服务。

during development, I have been using the full url for the service in order to make the service calls. 在开发过程中,我一直在使用服务的完整URL来进行服务调用。

example: 例:

http://myserver.com/appservices/service1.svc/getdata 

But now I need to do the following: 但现在我需要做以下事情:

  1. I have a domain on godaddy, lets say somedomain.com , and I want to have a subdomain services.somedomain.com to use instead of the full url. 我在godaddy上有一个域名,让我们说somedomain.com ,我希望有一个子域名services.somedomain.com来代替完整的url。 So I want to have something like services.somedomain.com/getdata instead of writing the full url above. 所以我希望有类似services.somedomain.com/getdata东西,而不是写上面的完整网址。 I was not able to do this. 我无法做到这一点。 I tried to setup URL Forwarding on Godaddy for the subdomain services to forward to http://myserver.com/appservices/service1.svc , this works on the browser, but when using it on the mobile application or through fiddler I get a 301 error. 我尝试在Godaddy上设置URL转发,以便子域服务转发到http://myserver.com/appservices/service1.svc ,这可以在浏览器上运行,但是当在移动应用程序上使用它或通过fiddler我得到301时错误。 (I tried both with and without masking). (无论是否有遮蔽,我都试过了)。 Is there a way to achieve that? 有没有办法实现这一目标?

  2. I need to get an SSL certification for it. 我需要获得SSL认证。 My question here is, for which domain should I get it? 我的问题是,我应该从哪个域获得它? do I get it for the somedomain.com or for the subdomain services.somedomain.com or maybe for the original domain myserver.com . 我是为somedomain.com或subdomain services.somedomain.com获取它,还是为原始域myserver.com获取它。 I'm a bit lost here, and I don't want to buy and ssl certificate on the wrong one 我有点迷失在这里,我不想买错ss证书

To give you an overall idea of the final result I'm hoping to get, I need to have the following: 为了让您全面了解我希望得到的最终结果,我需要具备以下条件:

somedomain.com points to a website deployed on iis somedomain.com指向在iis上部署的网站

services.somedomain.com points to a wcf webservice deployed on the same iis, and it will be used for services that will be called from android/iphone. services.somedomain.com指向部署在同一个iis上的wcf webservice,它将用于将从android / iphone调用的服务。 And this needs to have an SSL Certification 这需要获得SSL认证

I have bought somedomain.com from godaddy 我从godaddy买了somedomain.com

Thanks a lot for any hint/help I really appreciate it 非常感谢任何提示/帮助我真的很感激

Now I have a better understanding of the situation from the above questions. 现在我从上述问题中对情况有了更好的了解。

Firstly, as you own both the domains and you are the administrator of the Windows 2012 server, I would recommend that you add somedomain.com to your server directly, ie do not redirect it . 首先,由于您拥有两个域并且您是Windows 2012服务器的管理员 ,我建议您直接将somedomain.com添加到您的服务器, 即不要重定向它 This is better as you only need to purchase the SSL certificate that you need. 这样做更好,因为您只需购买所需的SSL证书。 If you used a redirect, you would need a SSL certificate for both domains as otherwise your users would get security warnings as the original request would not match the certificate. 如果您使用了重定向,则需要两个域的SSL证书,否则您的用户将收到安全警告,因为原始请求与证书不匹配。

Also what you want is Url Rewrite, not Redirect. 你想要的还有Url Rewrite,而不是Redirect。 What's the difference? 有什么不同? A redirect is a client-side request to have the web browser go to another website (using the 301 error code ). 重定向是一个客户端请求 ,让Web浏览器转到另一个网站(使用301错误代码 )。 A rewrite is a server-side rewrite of the URL before it reaches your service. 重写是在服务器到达您的服务之前对服务器端重写 URL。

You can get started with Url Rewriting add the following to your Web.config file: 您可以开始使用Url重写将以下内容添加到Web.config文件中:

<system.webServer>
  <rewrite>
    <rules>
      <!-- My rules -->
    </rules>
  </rewrite>
</system.webServer>

We are going to add a few rules (assuming that you have services.somedomain.com on the same server). 我们将添加一些规则(假设您在同一台服务器上有services.somedomain.com )。

  1. The first rule, rewrite http://services.somedomain.com/appservices/service1.svc/getdata to http://services.somedomain.com/getdata . 第一条规则,将http://services.somedomain.com/appservices/service1.svc/getdata重写为http://services.somedomain.com/getdata

     <rule name="SubDomainAppService" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^services\\.somedomain\\.com$" /> </conditions> <action type="Rewrite" url="{HTTPS}services.somedomain.com/appservices/service1.svc/{R:1}" /> </rule> 

    This will match where the regex pattern services.somedomain.com/getdata , capturing the remaining part of the url and internally rewrite it to services.somedomain.com/appservices/service1.svc/getdata . 这将匹配正则表达式模式services.somedomain.com/getdata ,捕获url的剩余部分并在内部将其重写为services.somedomain.com/appservices/service1.svc/getdata This rule also forces/rewrites the request to HTTPS . 此规则还强制/重写HTTPS请求。

  2. To force SSL for your website, you can do this by adding rewrite rule. 要为您的网站强制使用SSL,您可以通过添加重写规则来执行此操作。 This will rewrite any incoming request on HTTP to HTTPS . 这将重写HTTP上的任何传入请求到HTTPS

     <rule name="Redirect to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> 

URL Rewriting is a complex topic, not even one I understand fully. URL重写是一个复杂的主题,甚至不是我完全理解的主题。 A few resources that I read whilst answering this question: 我在回答这个问题时读到的一些资源:


Bonus: 奖金:

For your DNS, domain, and SSL certificates I would recommend using a better provider like DNSimple . 对于您的DNS,域和SSL证书,我建议使用更好的提供商,如DNSimple Read Troy Hunt's* post why he moved . 阅读Troy Hunt的帖子,为什么他搬家了 A SSL certificate from DNSimple for sub-domain and wildcard is on their site . 来自DNSimple的子域和通配符的SSL证书位于其站点上

* If you don't know who Troy Hunt is, read the rest of his articles on security. *如果你不知道Troy Hunt是谁,请阅读他关于安全性的其他文章。 Especially his posts on hacking your API first. 特别是他的帖子首先攻击你的API。

You need to configure your server so that it hosts two websites: 您需要配置服务器以便它托管两个网站:

  1. somedomain.com with bindings: somedomain.com绑定:
    • protocol: http, hostname: somedomain.com, port: 80 protocol:http,hostname:somedomain.com,port:80
    • protocol: http, hostname: www.somedomain.com, port: 80 协议:http,主机名:www.somedomain.com,端口:80
  2. services.somedomain.com with bindings: 带有绑定的services.somedomain.com:
    • protocol: https, hostname: services.somedomain.com, port: 443 protocol:https,hostname:services.somedomain.com,port:443
    • protocol: http, hostname: services.somedomain.com, port: 80 protocol:http,hostname:services.somedomain.com,port:80

(the last binding will simply be used to redirect http traffic to https). (最后一个绑定将仅用于将http流量重定向到https)。

SSL: SSL:

You must install a SSL certificate in order to create SSL bindings in IIS. 您必须安装SSL证书才能在IIS中创建SSL绑定。 The SSL certificates contain information about the hostname(s) for which the certificate was issued. SSL证书包含有关颁发证书的主机名的信息。 A certificate issued to foo.com cannot be used to identify bar.com . 颁发给foo.com的证书不能用于识别bar.com Make sure that you explain your needs to the certificate provider if you need to secure more than one hostname (in which case they would recommend UCC or wildcard certificates). 如果需要保护多个主机名,请确保向证书提供商说明您的需求(在这种情况下,他们会建议使用UCC或通配符证书)。

Also note that SSL host header configuration on IIS7 can only be done through appcmd command line. 另请注意,IIS7上的SSL主机头配置只能通过appcmd命令行完成。

Deployment: 部署:

Setup the two websites in separate directories (one contains the www content, the other contains the webservice files). 在单独的目录中设置两个网站(一个包含www内容,另一个包含webservice文件)。 Once the website and webservice files are separated you can choose to place the webservice files in the root. 将网站和Web服务文件分开后,您可以选择将Web服务文件放在根目录中。

URL Rewriting: URL重写:

In case you want to eliminate service1.svc from the URLs, you can use URL rewriting. 如果您想从URL中删除service1.svc ,可以使用URL重写。 It requires URL Rewrite module installed. 它需要安装URL Rewrite模块 You can use the IIS manager GUI to create the rules. 您可以使用IIS管理器GUI来创建规则。 Here is an example of the web.config file generated for services.somedomain.com: 以下是为services.somedomain.com生成的web.config文件的示例:

<system.webServer>
    <!-- other rules -->
    <rewrite>
        <rules>
            <rule name="http to https" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Temporary" />
            </rule>
            <rule name="everything to service1.svc">
                <match url="(.*)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="/appservices/service1.svc/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Since you ask several questions I hope to answer some of them: 既然你问几个问题我希望回答其中一些问题:

  1. Check first if your sub domain services.domain.com is properly forwarded by godaddy. 首先检查您的子域services.domain.com是否由godaddy正确转发。 Ping to the sub-domain to check if it hits your own server. Ping到子域以检查它是否到达您自己的服务器。 Than in IIS you add the web service with the host name services.domain.com. 在IIS中,您使用主机名services.domain.com添加Web服务。 For the website you add a separate web site with the host name: domain.com 对于该网站,您添加一个单独的网站,其主机名为:domain.com

  2. You have several options for the SSL Certificate, but the cheapest one would be to get it for the sub-domain: services.domain.com. 您有几种SSL证书选项,但最便宜的选择是获取子域名:services.domain.com。 In the web.config you can add a redirect like below to make sure the service always uses the HTTPS connection. 在web.config中,您可以添加如下所示的重定向,以确保服务始终使用HTTPS连接。

     <system.webServer> <rewrite> <rules> <rule name="Redirect to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> 

    enter code here

  1. Define on GoDaddy both somedomain.com and services.somedomain.com as "A" records with the IP address of your servers, and configure them as two sites on your servers. 在GoDaddy上将somedomain.comservices.somedomain.com定义为具有服务器IP地址的“A”记录 ,并将它们配置为服务器上的两个站点。 Don't redirect - even if you got it working it will slow down your users and cost you an extra certificate. 不要重定向 - 即使你工作它会降低你的用户速度,并花费你额外的证书。

  2. You need two certificates - one for somedomain.com and the other for *.somedomain.com . 您需要两个证书 - 一个用于somedomain.com ,另一个用于*.somedomain.com Note the wildcard - this will allow you to use the same certificate for additional sub-domains should you need them going forward. 请注意通配符 - 如果您需要它们,这将允许您为其他子域使用相同的证书。

  3. In a few months we expect to see free SSL - check out Let's Encrypt , so better not commit long term. 在几个月内,我们希望看到免费的SSL - 检查一下Let's Encrypt ,所以最好不要长期使用。

  4. You can probably avoid the URL rewrite by simply setting /appservices/service1.svc as your site's default, assuming all services.somedomain.com services are served via this entrypoint. 假设所有services.somedomain.com服务都是通过此入口点提供的,您可以通过简单地将/appservices/service1.svc设置为站点的默认值来避免URL重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM