wcf使用用户名和密码来验证

ValidateService(project)

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValidateService
{
    public class CalculateService:ICalculate
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValidateService
{
    [System.ServiceModel.ServiceContract]
    public interface ICalculate
    {
        [System.ServiceModel.OperationContract]
        int Add(int a, int b);
    }
}
using System;
using System.Collections.Generic;
using System.IdentityModel.Selectors;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValidateService
{
    public class MyValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (userName != "lizhch")
            {
                throw new Exception("username is wrong");
            }
        }
    }
}

ValidateHost(project)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValidateHost
{
    class Program
    {
        static void Main(string[] args)
        {
            System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(ValidateService.CalculateService));
            try
            {
                host.Open();
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.Write("serivce have started..");
                Console.Read();
            }
            catch (Exception ex)
            {
                host.Abort();
            }
            finally 
            {
                host.Close();
            }
            
            
            
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>


  <system.serviceModel>
    <services>
      <service name="ValidateService.CalculateService" behaviorConfiguration="ValidateServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8888/"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="ValidateService.ICalculate"></endpoint>
        
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ValidateServiceBehavior">
          <serviceMetadata  httpGetEnabled="true"/>
          <serviceCredentials>
            <userNameAuthentication customUserNamePasswordValidatorType="ValidateService.MyValidator,ValidateService"  userNamePasswordValidationMode="Custom"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttp">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic"/> <!--基于用户名和密码的验证 -->
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

client(project)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValidateClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceReference1.CalculateClient c = new ServiceReference1.CalculateClient())
            {
                c.ClientCredentials.UserName.UserName = "lizhch";
                c.ClientCredentials.UserName.Password = "tes";

                var result= c.Add(1, 2);
                Console.WriteLine(result.ToString());
                Console.Read();
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICalculate">
                    <security mode="TransportCredentialOnly">
                        <transport clientCredentialType="Basic" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8888/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ICalculate" contract="ServiceReference1.ICalculate"
                name="BasicHttpBinding_ICalculate" />
        </client>
    </system.serviceModel>
</configuration>

 

posted @ 2013-10-30 00:05  feidaochuanqing  阅读(333)  评论(0编辑  收藏  举报