Android WCF服务WsHttpBinding TransportWithMessageCredential连接

时间:2018-03-28 06:21:31

标签: java android wcf credentials wshttpbinding

连接经过身份验证 WCF 服务并使用 WsHttpBinding

private void Show()
{
    MobilServiceClient client = new MobilServiceClient();
    client.ClientCredentials.UserName.UserName = "admin";
    client.ClientCredentials.UserName.Password = "adminpass";
    MessageBox.Show(client.LoginServiceTest());
}

此代码有效。但我无法将服务与java连接。

Ksoap2:

public static String WCFShow (String METHOD_NAME, int timeOut, List<KSoapParameters> parameters)  {

    SoapObject request = new SoapObject("http://tempuri.org/", "LoginServiceTest");
    for (int i =0;i<parameters.size();i++){
        request.addProperty(parameters.get(i).getKey(),parameters.get(i).getValue());
    }
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.dotNet = true;
    List<HeaderProperty> header = new ArrayList<HeaderProperty>();
    header.add(new HeaderProperty("Authorization","Basic "+org.kobjects.base64.Base64.encode("admin:adminpass".getBytes())));
    envelope.setOutputSoapObject(request);

    Element headers[] = new Element[3];
    headers[0]= new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
    headers[0].setAttribute(envelope.env, "mustUnderstand", "1");
    Element security=headers[0];

    Element token = new Element().createElement(security.getNamespace(), "UsernameToken");
    token.setAttribute("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id", "UsernameToken-2");

    Element username = new Element().createElement(security.getNamespace(), "Username");
    username.addChild(Node.TEXT, "admin");
    token.addChild(Node.ELEMENT,username);

    Element password = new Element().createElement(security.getNamespace(), "Password");
    password.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
    password.addChild(Node.TEXT, "adminpass");
    token.addChild(Node.ELEMENT,password);

    Element ee = new Element();
    ee.setName("To");
    ee.setNamespace("http://www.w3.org/2005/08/addressing");
    ee.addChild(Node.TEXT,
            "https://xxx.xxx.com.tr/MobilServis/mbFunctions.svc");


    Element e1 = new Element();
    e1.setName("Action");
    e1.setNamespace("http://www.w3.org/2005/08/addressing");
    e1.addChild(Node.TEXT,
            "http://tempuri.org/ImbFunctions/"+"LoginServiceTest");

    headers[0].addChild(Node.ELEMENT, token);
    headers[1] = new Element();
    headers[1].addChild(Node.ELEMENT,ee);
    headers[2] = new Element();
    headers[2].addChild(Node.ELEMENT,e1);

    envelope.headerOut = headers;

    HttpTransportSE aht = new HttpTransportSE("https://xxx.xxx.com.tr/MobilServis/mbFunctions.svc",timeOut);
    aht.debug = true;

    try {
        aht.call("http://tempuri.org/ImbFunctions/"+"LoginServiceTest", envelope, header);
        Log.d("Erdinc", envelope.getResponse().toString());
        //Object result = envelope.getResponse();
        Object result = envelope.bodyIn;

        resultData = result.toString();
    } catch (IOException e) {
        e.printStackTrace();
        resultData="ERROR"+e.toString();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
        resultData="ERROR"+e.toString();
    }

    return resultData;
}

安全模式= TransportWithMessageCredential

传输ClientCredentialType =基本

消息ClientCredentialType = UserName

绑定类型= WsHttpBinding

的web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>

  <system.web>
<compilation debug="true" />
<membership defaultProvider="ClientAuthenticationMembershipProvider">
  <providers>
    <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
  </providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
  <providers>
    <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  </providers>
</roleManager>
  </system.web>
  <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>

<directoryBrowse enabled="true"/>
  </system.webServer>
  <system.serviceModel>
    <bindings>
  <wsHttpBinding>
    <binding name="wsHttp" maxReceivedMessageSize="2147483647" >

      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Basic"/>
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<services>
  <service name="CGMobilServices.mbFunctions" behaviorConfiguration="wsHttpBehavior">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="CGMobilServices.ImbFunctions">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="http://192.168.10.92"/>
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="wsHttpBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="False"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="CGMobilServices.UserNamePassValidator, CGMobilServices"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

我在等你的帮助

0 个答案:

没有答案