如何在WCF SessionMode.Required中使用HTTPS - 最简单的可能示例

时间:2014-07-31 17:08:04

标签: c# wcf https wcf-binding

更新(2014年8月7日) - 这个问题的解决方案是我需要添加一个派生自“UserNamePasswordValidator”的类并在Web.Config中注册它。

我创建了一个简单的测试WCF服务和测试控制台客户端应用程序(请参阅下面的代码)。我使用的是.NET 4.5.1。我已经在StackOverflow上搜索了重复项(找到类似的帖子herehere) - 但是我觉得引用的帖子可能已经过时了,而且我觉得我的帖子范围也比较有限。

现在可以举例:

解决方案目前使用会话(在ITestService.cs中):

[ServiceContract(SessionMode = SessionMode.Required)]

...并使用wsHttpBinding(参见下面的app.config和web.config)。

当我将其部署到服务器时,我已成功地使用HTTPS通过网络浏览器访问它,如下所示:https://myserver.com/test/testservice.svc

但是,当我从客户端app.config更改端点时:

http://localhost:20616/TestService.svc/TestService.svc

为:

https://myserver.com/test/testservice.svc

再次运行控制台应用程序,我收到错误:“提供的URI方案'https'无效;预期'http'。参数名称:via”

我的问题是,在不更改SessionMode.Required的情况下,我需要做的最小变化是什么?

这是客户端控制台应用程序代码。请务必将“mycomputer \ Matt”的App.Config值更改为您机器的正确值。

Program.cs的

using System;

namespace TestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();
            Console.WriteLine("Attempting to log in...");
            try
            {
                TestServiceReference.TestServiceClient client = new TestServiceReference.TestServiceClient();

                bool loginSuccess = client.LogIn("admin", "password");
                if (loginSuccess)
                {
                    Console.WriteLine("Successfully logged in.");
                    string secretMessage = client.GetSecretData();
                    Console.WriteLine("Retrieved secret message: " + secretMessage);
                }
                else
                {
                    Console.WriteLine("Log in failed!");
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception occurred: " + exc.Message);
            }
            Console.WriteLine("Press ENTER to quit.");
            Console.ReadLine();
        }
    }
}

App.config中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ITestService"/>
            </wsHttpBinding>
        </bindings>
        <client>
          <endpoint address="https://myserver.com/test/testservice.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITestService" contract="TestServiceReference.ITestService" name="WSHttpBinding_ITestService">
            <identity>
              <userPrincipalName value="mycomputer\Matt"/>
            </identity>
          </endpoint>
            <!--<endpoint address="http://localhost:20616/TestService.svc/TestService.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITestService" contract="TestServiceReference.ITestService" name="WSHttpBinding_ITestService">
                <identity>
                    <userPrincipalName value="mycomputer\Matt"/>
                </identity>
            </endpoint>-->
        </client>
    </system.serviceModel>
</configuration>

WCF服务代码。
ITestService.cs:

using System.ServiceModel;

namespace WcfSessionsOverHttpsTest
{
    [ServiceContract(SessionMode = SessionMode.Required)]
    public interface ITestService
    {

        [OperationContract(IsInitiating = true)]
        bool LogIn(string username, string password);

        [OperationContract(IsInitiating = false, IsTerminating = true)]
        bool LogOut();

        [OperationContract(IsInitiating = false)]
        string GetSecretData();
    }
}

TestService.svc:

namespace WcfSessionsOverHttpsTest
{    
    public class TestService : ITestService
    {
        public bool IsAuthenticated { get; set; }
        bool ITestService.LogIn(string username, string password)
        {
            if (username == "admin" && password == "password")
            {
                IsAuthenticated = true;
                return true;
            }
            else
            {
                IsAuthenticated = false;
                return false;
            }
        }

        bool ITestService.LogOut()
        {
            IsAuthenticated = false;
            return true;
        }

        string ITestService.GetSecretData()
        {
            if (!IsAuthenticated)
            {
                throw new System.Security.Authentication.AuthenticationException("User has not logged in.");
            }
            else
            {
                string secretMessage = "The Red Sox are going to win the World Series in 2016";
                return secretMessage;
            }
        }
    }
}

的Web.config:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpEndpointBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WcfSessionsOverHttpsTest.TestService">
        <endpoint address="/TestService.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="WcfSessionsOverHttpsTest.ITestService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="wsHttpBinding" scheme="http"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

提前感谢您的帮助!

马特

1 个答案:

答案 0 :(得分:0)

这个问题的解决方案是我需要添加一个派生自&#34; UserNamePasswordValidator&#34;并在Web.Config中注册。

public class CustomUserNameValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        return;
    }
}

的Web.config:

<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyProgram.CustomUserNameValidator,MyProgram" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>