【问题标题】:How to modify existing REST web service to work with SSL如何修改现有 REST Web 服务以使用 SSL
【发布时间】:2015-01-06 22:06:20
【问题描述】:

我根据MSDN 上的代码运行了以下简单的 REST 服务 - 代码如下。 如何修改它以使用传输安全性 - SSL? 我一直在谷歌搜索解决方案,但似乎大多数示例都提到修改 web.config 文件,但这个示例甚至没有那个......感谢您对此提供的任何帮助!

Imports System
Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Web
Imports System.Text

<ServiceContract()> _
Public Interface IService
    <OperationContract()> _
    <WebGet()> _
    Function EchoWithGet(ByVal s As String) As String

    <OperationContract()> _
    <WebInvoke()> _
    Function EchoWithPost(ByVal s As String) As String
  end interface

Public Class Service
    Implements IService
    Public Function EchoWithGet(ByVal s As String) As String Implements IService.EchoWithGet
        Return "You said " + s
    End Function

    Public Function EchoWithPost(ByVal s As String) As String Implements IService.EchoWithPost
        Return "You said " + s
    End Function
End Class

Module program

    Sub Main()
        Dim host As WebServiceHost = New WebServiceHost(GetType(Service), New Uri("http://localhost:8000/"))
        Try
            Dim ep As ServiceEndpoint = host.AddServiceEndpoint(GetType(IService), New WebHttpBinding(), "")
            host.Open()
            Using cf As New ChannelFactory(Of IService)(New WebHttpBinding(), "http://localhost:8000")

                cf.Endpoint.Behaviors.Add(New WebHttpBehavior())

                Dim channel As IService = cf.CreateChannel()

                Dim s As String

                Console.WriteLine("Calling EchoWithGet via HTTP GET: ")
                s = channel.EchoWithGet("Hello, world")
                Console.WriteLine("   Output: {0}", s)

                Console.WriteLine("")
                Console.WriteLine("This can also be accomplished by navigating to")
                Console.WriteLine("http://localhost:8000/EchoWithGet?s=Hello, world!")
                Console.WriteLine("in a web browser while this sample is running.")

                Console.WriteLine("")

                Console.WriteLine("Calling EchoWithPost via HTTP POST: ")
                s = channel.EchoWithPost("Hello, world")
                Console.WriteLine("   Output: {0}", s)
                Console.WriteLine("")
            End Using

            Console.WriteLine("Press <ENTER> to terminate")
            Console.ReadLine()

            host.Close()
        Catch cex As CommunicationException
            Console.WriteLine("An exception occurred: {0}", cex.Message)
            host.Abort()
        End Try
    End Sub

End Module

【问题讨论】:

    标签: vb.net web-services rest ssl


    【解决方案1】:

    添加以下内容解决了我的问题,所有流量都通过 HTTPS。我希望这对将来的某人有所帮助。

        Dim binding As New WebHttpBinding
    
        binding.Security.Mode = WebHttpSecurityMode.Transport
        Dim store As New X509Store(StoreName.My, StoreLocation.LocalMachine)
        store.Open(OpenFlags.ReadWrite)
        Dim cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", False)(0)
        store.Close()
    
        Dim bindPortToCertificate As New Process
        bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe")
        bindPortToCertificate.StartInfo.Arguments = String.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}", 22370, cert.Thumbprint, Guid.NewGuid())
        bindPortToCertificate.Start()
        bindPortToCertificate.WaitForExit()
    

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2011-10-03
      • 2015-04-25
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2016-06-25
      相关资源
      最近更新 更多