Visual Studio host ,主机上配置的身份验证方案 ('Anonymous')

Visual Studio host , The authentication schemes configured on the host ('Anonymous')

我遇到了错误

The authentication schemes configured on the host ('Anonymous') do not allow those configured on the binding 'WebHttpBinding' ('Basic').

当我运行 VS 中的服务时出现问题。

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6" />
    <httpRuntime targetFramework="4.6"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceX_Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ServiceX.CustomUserNamePasswordValidator, ServiceX"/>
          </serviceCredentials>
          <!--<serviceAuthenticationManager authenticationSchemes="Basic"/>-->
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpBinding_Behavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="ServiceX.ServiceX" behaviorConfiguration="ServiceX_Behavior">
        <endpoint address="" binding="webHttpBinding" contract="ServiceX.IServiceX" behaviorConfiguration="webHttpBinding_Behavior" />
        <endpoint address="soap" binding="basicHttpBinding" contract="ServiceX.IServiceX" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

当我在 IIS 中 运行 时,它工作正常。当我 运行 在控制台应用程序中时,它工作起来很有趣。与 VS 有什么区别? 以下工作正常,与上述配置相同

Uri serviceUri = new Uri("http://localhost/TestService");
ServiceHost serviceHost = new ServiceHost(typeof(TestService), serviceUri);


BasicHttpBinding basicHttp = new BasicHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IService), basicHttp, "soap");

ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(behavior);
serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");


var contract = ContractDescription.GetContract(typeof(IService));
contract.Namespace = "WCFServer";

var webHttpEndpoint = new WebHttpEndpoint(contract);
webHttpEndpoint.Address = new EndpointAddress(serviceUri);
var webHttpBinding = (WebHttpBinding)webHttpEndpoint.Binding;
webHttpBinding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
serviceHost.AddServiceEndpoint(webHttpEndpoint);

//serviceHost.Authentication.AuthenticationSchemes = AuthenticationSchemes.Basic;
serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNameAuthentication();

foreach (var bh in serviceHost.Description.Behaviors)
{
    var serviceMetadata = bh as ServiceMetadataBehavior;
    if (serviceMetadata != null)
    {
        serviceMetadata.HttpGetEnabled = true;
    }

    var serviceDebug = bh as ServiceDebugBehavior;
    if (serviceDebug != null)
    {
        serviceDebug.IncludeExceptionDetailInFaults = false;
    }
}
serviceHost.Open();
Console.ReadLine();
serviceHost.Close();

在 ISS 中允许基本身份验证和匿名身份验证
IN VS 启用匿名身份验证

为什么 Visual Studio WCF web 项目不起作用,控制台项目和部署在 IIS web 应用程序中工作正常。有什么区别?

我想在 WCF 中实现基本身份验证

根本原因是 IIS Express 默认没有启用 BasicAuthentication。如您所知,我们需要启用 BasicAuthentication 以支持 WCF BasicAuthentication。 IIS Express 配置有 ApplicationHost.config 文件。我们应该更新 ApplicationHost.config 中的以下值以启用 BasicAuthentication.

<security>
            <access sslFlags="None" />
            <applicationDependencies>
                <application name="Active Server Pages" groupId="ASP" />
            </applicationDependencies>
            <authentication>
                <anonymousAuthentication enabled="true" userName="" />
<!-- set to true –->
                <basicAuthentication enabled="true" />

VS2015及以上版本。 ApplicationHost.config位于下方。

Solution file directory(containing the Sln file)/.vs/solution name/applicationhost.config

如果问题仍然存在,请随时告诉我。