Web服务用户身份验证

时间:2014-06-30 11:44:49

标签: c# security authorization webservice-client

我正在尝试使用用户授权开发Web服务客户端。 我已经为我的项目添加了服务引用,但现在我坚持用户身份验证, 我得到的是:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'.

这就是我打电话给我的网络服务的方式:

BasicHttpBinding a = new BasicHttpBinding("MyWebService1");
a.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
a.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

MyWebService1Client mySeviceClient= new MyWebService1Client();
mySeviceClient= new MyWebService1Client(a, mySeviceClient.Endpoint.Address);

mySeviceClient.ClientCredentials.UserName.UserName = "test2";
mySeviceClient.ClientCredentials.UserName.Password = "password";

try
{
    mySeviceClient.Open();
    MyWebService1 myWebService= mySeviceClient;
    myRequest req = new myRequest(1234, "Test");
    myResponse res = myWebService.getMyData(req);
    mySeviceClient.Close();
}
catch (AddressAccessDeniedException adExc)
{
           Console.WriteLine(adExc.Message);
           Console.ReadLine();
}
catch (System.Exception exc)
{
    Console.WriteLine(exc.Message);
    Console.ReadLine();
}

这是我的app.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="MyWebService1"/>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:7002/WSauth-WS_auth-context-root/MyWebService1Port"
            binding="basicHttpBinding" bindingConfiguration="MyWebService1"
            contract="TestWs.MyWebService1" name="MyWebService1Port" />
    </client>
  </system.serviceModel>
</configuration>

0 个答案:

没有答案