• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Wcf

带基本身份验证通过帐号密码访问的Wcf
PHP:

$key="abc";
$password="123";
$url="http://127.0.0.1:1000";
$client = new SoapClient ($url,
    array('trace'=>true,'exception'=>0,
    'login'=>$key,'password'=>$password,
    'connection_timeout'=>60,
    'authentication'=>SOAP_AUTHENTICATION_BASIC));
$ret=$client->Add(array("num1"=>1,"num2"=>2));
var_dump($ret);

C#

Uri wcfPath = new Uri("http://127.0.0.1:1000/Namespace");
var bhb=new BasicHttpBinding();
bhb.AllowCookies = false;
bhb.TransferMode = TransferMode.Buffered;
bhb.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
bhb.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var client = new WcfTestClient(bhb,new EndpointAddress(wcfPath));
client.ClientCredentials.UserName.UserName = "abc";
client.ClientCredentials.UserName.Password = "123";

.Net Server

Type type=typeof(WcfTest);
Uri baseAddress = new Uri("127.0.0.1:1000/Normal");
Uri webAddress = new Uri("127.0.0.1:1000/Web");

var wcfHost = new ServiceHost(type);
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("127.0.0.1:1000");
wcfHost.Description.Behaviors.Add(behavior);
wcfHost.Description.Name = "serviceTest";

ContractDescription cd = ContractDescription.GetContract(type);
WebHttpBinding wb = new WebHttpBinding();
WebHttpBehavior whb = new WebHttpBehavior();

whb.DefaultBodyStyle = WebMessageBodyStyle.Wrapped;
whb.DefaultOutgoingResponseFormat = WebMessageFormat.Json;

ServiceEndpoint ep1 = new ServiceEndpoint(cd, wb, new EndpointAddress(webAddress));
ep1.Behaviors.Add(whb);
wcfHost.Description.Endpoints.Add(ep1);

BasicHttpBinding bhb = new BasicHttpBinding();
bhb.AllowCookies = false;
bhb.TransferMode = TransferMode.Buffered;
bhb.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
bhb.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

ServiceEndpoint ep = new ServiceEndpoint(cd, bhb, new EndpointAddress(baseAddress));
wcfHost.Description.Endpoints.Add(ep);
wcfHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
wcfHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
wcfHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new WcfValidator();

wcfHost.Open();

posted @ 2016-05-24 15:09  HaibaraAi  阅读(275)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3