具有基本身份验证问题的WCF

本文关键字:问题 WCF 身份验证 | 更新日期: 2023-09-27 18:16:30

我几乎没有尝试为我的WCF设置basicauthentication(发布在IIS 8.5上)。但是我总是得到这些错误之一:

http请求被禁止使用客户端身份验证方案'basic'。从服务器获得了以下authenticationheader "Digest qop="auth",algorithm=MD5-sess,nonce="someMD5stuff",charset=utf-8,realm="Digest",Negotiate,NTLM,Basic realm="localhost"。

客户端身份验证方案为"基本",HTTP请求被禁止。从服务器获取了authenticationheader "Basic realm="localhost" .

我的网页。配置服务器端(在WCF中):

<system.serviceModel>
<services>
  <service name="WCF_for_APP.Service1">
    <endpoint
      address=""
      binding="basicHttpBinding"
      contract="WCF_for_APP.Service"/>
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IPersonService" />
    <binding>
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" proxyCredentialType="None" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://somewhere/customerService"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPersonService"
    contract="PersonService.IPersonService" name="BasicHttpBinding_IPersonStateService" />
</client>
<behaviors>
  <serviceBehaviors>
    <behavior>        
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>  
      <serviceAuthenticationManager authenticationSchemes="Basic"></serviceAuthenticationManager>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="CustomerValidator.SecureBindingUsernamePasswordValidator, CustomerValidator" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.servicemodel>

我已经尝试过为bindingConfiguration等设置一个名称,但它没有改变任何事情。

我尝试在ASP应用程序中通过Channelfactory客户端访问我的WCF:

EndpointAddress endpointAddress = new EndpointAddress(endpointadress);
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.ReaderQuotas.MaxBytesPerRead = Int16.MaxValue;
basicHttpBinding.MaxReceivedMessageSize = int.MaxValue;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
ChannelFactory<Service> channelFactory = null;
Service client = null;            
channelFactory = new ChannelFactory<Service>(basicHttpBinding, endpointAddress);
channelFactory.Credentials.UserName.UserName = ConfigurationManager.AppSettings["wcfUser"].ToString();
channelFactory.Credentials.UserName.Password = ConfigurationManager.AppSettings["wcfPW"].ToString();
try
{
    client = channelFactory.CreateChannel();
    string a = client.SendMail();
}
catch(Exception e)
{
    Response.Write(e.Message);
}

激活IIS的基本身份验证。我真的不想切换到HTTPS和证书,因为我只需要这个基本的内部安全认证。我甚至不能用visual studio WCF testclient启动WCF,但没有身份验证设置一切都很好。WCF和ASP都在我的本地IIS上发布。

有什么建议吗?我必须向我的本地系统添加具有相同凭据的用户吗?

** EDIT **

我想我知道问题了!在我的WCF中,我打电话给另一个WCF(来自客户),这个WCF与我的安全设置相结合会产生麻烦。怎么解呢?我的WCF与服务器端配置和客户端配置的WCF在一个web.config?(客户WCF也通过ChannelFactory调用)因为如果我命名了bindingconfigs,它不会改变任何东西(见上面的代码)。

通过ChannelFactory调用客户WCF的代码类似于上面的代码。这就是问题所在吗?2 WCF -一个服务器端和一个调用端?!

具有基本身份验证问题的WCF

我的basicauthentication一切正常。

错误是我通过我的WCF调用的另一个WCF,由于我的WCF中的安全设置,我无法调试(不是挂起处理或测试客户端),并且由于错误消息中没有详细信息,我没有找到此错误的来源。这另一个WCF抛出错误与"基本"是不允许的,所以我的客户在他的IIS上做了一些更改(可能)。

并且我必须添加与我在WCF中使用的凭据相同的本地用户。我不知道,但是,是的,一切都很好,我必须等待我的客户告诉我他们的设置。

我创建了测试项目,它正在工作。代码如下:

IService1接口:

namespace WcfTestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

Service1类:

namespace WcfTestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

网络。配置文件:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfTestService.Service1" behaviorConfiguration="HttpBehavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="WcfTestService.IService1" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding"  >
          <readerQuotas />
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" realm="" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HttpBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

启用IIS基本身份验证,禁用匿名。

WCF客户机控制台应用程序:

namespace WcfTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            EndpointAddress endpointAddress = new EndpointAddress(@"http://localhost/Service1.svc");
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            basicHttpBinding.MaxReceivedMessageSize = int.MaxValue;
            basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            var channelFactory = new ChannelFactory<WcfTestService.IService1>(basicHttpBinding, endpointAddress);
            channelFactory.Credentials.UserName.UserName = @"server'someuser";
            channelFactory.Credentials.UserName.Password = @"somepass";
            try
            {
                var client = channelFactory.CreateChannel();
                string a = client.GetData(55);
                Console.Write(e.Message);
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }
    }
}

下载:http://sharesend.com/6gutdu4s