WCF自定义身份验证 - HTTP状态401:未经授权

时间:2010-12-19 10:05:56

标签: c# wcf security

WCF服务WebConfig(部分)。

    <services>
          <service name="Bricks99.LicensingServer.LicensingService"
                   behaviorConfiguration="Bricks99ServiceBehavior">        
            <!-- use base address specified above, provide one endpoint -->
            <endpoint address=""
                      binding="basicHttpBinding"
                      bindingConfiguration="Bricks99Binding"
                      contract="Bricks99.LicensingServer.ILicensingService" />
            <!--<endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />-->
          </service>
    </services>
    <basicHttpBinding>
            <binding name="Bricks99Binding">
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" />
              </security>
            </binding>
    </basicHttpBinding>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Bricks99.LicensingServer.CCustomValidatorClass, Bricks99.LicensingServer"/>
</serviceCredentials>

客户端是ASP.NET 2.0

LicensingService service = new LicensingService();
            //Authentication            
CredentialCache credCache = new CredentialCache();
            credCache.Add(new Uri(service.Url), "Basic", new NetworkCredential("Test", "1234567"));
service.Credentials = credCache;
service.PreAuthenticate = true;
service.UseDefaultCredentials = false;
result = service.VerifyLicenseKey(licenseKey, string.Empty);

结果总是请求失败,HTTP状态为401:未经授权。我也关闭了文件夹上的匿名访问。它仍然无法正常工作。 有关如何正确设置凭据的任何想法?

编辑:看起来像被覆盖的方法

public override void Validate(string userName, string password)
        {
            try
            {
                if(userName != "Test" || password != "1234567")
                {
                    throw new FaultException("The provided credentials are invalid.");
                }
            }
            catch(FaultException ex)
            {
                LicensingData.Operations.LogError(ex);                
            }
        }

永远不会被击中。

1 个答案:

答案 0 :(得分:2)

经过数小时的研究后,我发现主机提供商默认情况下不允许基本身份验证。要使自定义身份验证起作用,必须在IIS中启用基本身份验证。