如何使用HTTPS与WCF SessionMode.必选-最简单的例子

本文关键字:最简单 必选 SessionMode 何使用 HTTPS WCF | 更新日期: 2023-09-27 18:15:55

UPDATE(8/7/2014) -解决这个问题的方法是,我需要添加一个类,派生自"UserNamePasswordValidator"并在Web.Config中注册它。

我已经创建了一个简单的测试WCF服务和测试控制台客户端应用程序(见下面的代码)。我使用的是。net 4.5.1。我已经在StackOverflow上搜索了重复的内容(在这里和这里发现了类似的帖子)-但是我觉得引用的帖子可能已经过时了,也觉得我的帖子在范围上更有限。

好了,现在来看看这个例子:

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

[ServiceContract(SessionMode = SessionMode.Required)]

…并使用wsHttpBinding(见下文app.config和web.config)。

当我将其部署到服务器时,我能够通过web浏览器使用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>

提前感谢您的帮助!

马特

如何使用HTTPS与WCF SessionMode.必选-最简单的例子

解决这个问题的方法是,我需要添加一个派生自"UserNamePasswordValidator"的类,并在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>