Android中的Windows身份验证 [英] Windows authentication in android

查看:57
本文介绍了Android中的Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中使用WCF Web服务...我必须通过Windows身份验证对用户进行身份验证这是我的Java代码

I am using WCF webservice in android... i've to authenticate the user with windows authentication Here's my java code

public void test3()
{
       try
       {
               DefaultHttpClient httpClient = new DefaultHttpClient();
               URI uri = new URI("http://10.0.2.2/MicrosoftDynamicsAXAif60/testgroup/xppservice.svc"); 

                       // Send GET request to <service>/GetText
                       HttpPost httpget = new HttpPost(uri);
                       StringEntity se = null;

                        // declare it as XML
                       se = new StringEntity(this.returnXML(), HTTP.UTF_8);

                       se.setContentType("text/xml");
                       httpget.setHeader("Content-Type", "application/xml;charset=UTF-8");
                       httpget.setEntity(se);

                       //NTCredentials nt = new NTCredentials("username", "pass", "", "domain");
                       httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, nt);


                       HttpResponse response = httpClient.execute(httpget);
                       HttpEntity responseEntity = response.getEntity();

                       // Read response data into buffer
                       long intCount = responseEntity.getContentLength();
               char[] buffer = new char[(int)intCount];
               InputStream stream = responseEntity.getContent();
               InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
               try
               {
                       reader.read(buffer);


                       String str = new String(buffer);
                       Toast.makeText(this, str, 10);
               }

               catch (IOException e) 
               {e.printStackTrace();}
               stream.close();


       } 
       catch (Exception e) {e.printStackTrace();}

   }

现在收到HTTP错误401.2-未经授权

Now am getting HTTP Error 401.2 - Unauthorized

这是xml配置

      <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <system.serviceModel>
      <bindings>
        <wsHttpBinding>
          <binding name="wsHttpBindingWithWindowsAuth">
              <security mode="TransportWithMessageCredential">
                  <transport clientCredentialType="Windows" >
                      <extendedProtectionPolicy protectionScenario="TransportSelected" policyEnforcement="Always" />
                  </transport>
                  <message clientCredentialType="Windows" />
              </security>
          </binding>
        </wsHttpBinding>
          <basicHttpBinding>
              <binding name="basicHttpBindingWithWindowsAuth">
                  <security mode="TransportCredentialOnly">
                      <transport clientCredentialType="Windows" />
                  </security>
              </binding>
          </basicHttpBinding>
          <netTcpBinding>
          <binding name="DefaultServiceGroupBinding" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="81920000" maxConnections="10" maxReceivedMessageSize="81920000">
            <readerQuotas maxDepth="32000000" maxStringContentLength="81920000" maxArrayLength="163840000" maxBytesPerRead="4096000" maxNameTableCharCount="16384000" />
            <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
            <security mode="Transport">
              <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
              <message clientCredentialType="Windows" />
            </security>
          </binding>
        </netTcpBinding>
      </bindings>

      <services>
        <!--ROUTING SERVICE -->
        <!--Define a routing service.  The Behavior Configuration field references the name of the Routing Behavior that this service will use-->
        <service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="routingData">
          <!--
            Define and configure the endpoint we want the Router to listen on and the Contract we want it to use
            Router provided contracts are:  ISimplexDatagramRouter, ISimplexSessionRouter, IRequestReplyRouter, and IDuplexSessionRouter.          
           -->
          <endpoint address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="basicHttpBindingWithWindowsAuth"
                    name="reqReplyEndpoint"
                    contract="System.ServiceModel.Routing.IRequestReplyRouter" >
            <identity>
              <servicePrincipalName />
            </identity>
          </endpoint>
        </service>
      </services>

      <behaviors>
        <serviceBehaviors>
          <behavior name="routingData">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
            <serviceDebug includeExceptionDetailInFaults="False" />
            <!--
            Define the Routing Behavior.  The routingTableName attribute contains the name of the Routing Table that this behavior will use.          
            <routing filterTableName="AosRoutingTable" />
            -->
          </behavior>
        </serviceBehaviors>

        <endpointBehaviors>
          <behavior name="clientEndpointBehavior">
            <clientEndpointBehavior />
          </behavior>
        </endpointBehaviors>
      </behaviors>

      <extensions>
        <behaviorExtensions>
          <add
              name="clientEndpointBehavior"
              type="Microsoft.Dynamics.Ax.Services.Host.ClientEndpointBehaviorExtension, Microsoft.Dynamics.IntegrationFramework.WebService.Process, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"   />
        </behaviorExtensions>
      </extensions>

      <!--
        Define the client endpoints that we want the Router to communicate with.  These are the destinations that the Router will send messages to.
        Note that we always define the client endpoint contract as *.  The router will mirror the selected contract defied on the endpoint 
        on which the message was originally recieved.
      -->
      <client>
                  <endpoint name="ServiceCustom" address="net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/testgroup" binding="netTcpBinding" bindingConfiguration="DefaultServiceGroupBinding" contract="*" behaviorConfiguration="clientEndpointBehavior">
              <identity><servicePrincipalName /></identity>
          </endpoint>    

      </client>
        <routing>
            <!--
          Define the filters that we want the router to use.  In this example we define a MatchAll message filter, which will match all messages it sees.
        -->
            <filters>
                        <filter name="MatchServiceCustom" filterType="Custom" customType="Microsoft.Dynamics.Ax.Services.Host.MatchAxServiceFilter, Microsoft.Dynamics.IntegrationFramework.WebService.Process" filterData="http://tempuri.org/ServiceCustom" />

            </filters>

            <!-- Define the routing table that contains the matchAll filter defined in the filters section. -->
            <filterTables>
                <filterTable name="AosRoutingTable">
                    <!-- Map the filter to a client endpoint that was previously defined. Messages that match this filter will be sent to this destination. -->
                            <add filterName="MatchServiceCustom" endpointName="ServiceCustom" />

                </filterTable>
            </filterTables>
        </routing>
    </system.serviceModel>
  </configuration>

我要发送的xml

<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action>http://tempuri.org/ServiceCustom/CreateMyDataObject</a:Action>
<h:CallContext i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:h="http://schemas.microsoft.com/dynamics/2010/01/datacontracts" />
<a:MessageID>urn:uuid:c70fb5f6-1345-469d-84fd-bb24500d329a</a:MessageID>
<a:ReplyTo>
  <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
<s:Body>
<ServiceCustomCreateMyDataObjectRequest xmlns="http://tempuri.org">
<_s>23</_s>
</ServiceCustomCreateMyDataObjectRequest>
</s:Body>
</s:Envelope>

已解决!

http://mrrask.wordpress.com/2009/08/21/android-authenticating-via-ntlm/

推荐答案

(OP通过发布Web链接来回答.转换为社区Wiki答案;

(The OP answered by posting a web link. Converted to a Community Wiki Answer; Question with no answers, but issue solved in the comments (or extended in chat) )

链接为: http://mrrask.wordpress.com/2009/08/21/android-authenticating-via-ntlm/

摘要为:

它使用一个HttpClient,它需要一个AuthScemeFactory,它将返回AuthScheme后代NTLMScheme.NTLMScheme需要NTLMEngine接口的实现才能进行操作.JCIFS库具有用于创建类型1 2和版本1的功能.3条NTLM消息使其成为实施NTLMEngine的理想选择.

It uses an HttpClient which needs an AuthScemeFactory that will return the AuthScheme descendant NTLMScheme. NTLMScheme requires an implementation of the NTLMEngine interface to operate on. The JCIFS library has functionality for creating type 1 2 & 3 NTLM messages making it a perfect candidate for implementing the NTLMEngine.

这篇关于Android中的Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆