Android 與 IIS伺服器身分識別驗證

來源:互聯網
上載者:User

標籤:

1)基礎驗證:

 1 /** 2      * 從伺服器取圖片 3      *  4      * @param url 5      * @return 6      */ 7     public void getHttpBitmap(final String url) { 8         new Thread(new Runnable() { 9             public void run() {10                 Bitmap bitmap = null;11                 try {12                     HttpGet httpPost = new HttpGet(url);13                     DefaultHttpClient httpClient = new DefaultHttpClient();14                     // 基本驗證15                     BasicCredentialsProvider bcp = new BasicCredentialsProvider();16                     String userName = "username";17                     String password = "password";18                     AuthScope authScope = new AuthScope(AuthScope.ANY);19                     bcp.setCredentials(authScope ,20                       new UsernamePasswordCredentials(userName, password));21 22                     httpClient.setCredentialsProvider(bcp);23                     
              //基本驗證結束
             HttpResponse httpResponse = httpClient.execute(httpPost);24 android.util.Log.i("", "=====>" + httpResponse.getStatusLine().getStatusCode());25 android.util.Log.i("", "=====>" + httpResponse.getEntity().getContentLength());26 InputStream is = httpResponse.getEntity().getContent();27 bitmap = BitmapFactory.decodeStream(is);28 is.close();29 } catch (Exception e) {30 e.printStackTrace();31 }32 33 getResult(bitmap);34 }35 }).start();36 }
1 byte[] encodedPassword = (userName + ":" + password).getBytes();2 httpPost.addHeader("Authorization","Base " + Base64.encodetoString(encodedPassword, Base64.No_Wrap);

2)ntlm 身分識別驗證:

1  // register ntlm auth scheme2                     httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());3                     httpClient.getCredentialsProvider().setCredentials(4                             // Limit the credentials only to the specified domain and port5                             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),6                             // Specify credentials, most of the time only user/pass is needed7                             new NTCredentials(userName, password, "", "")8                     );

直接替換 //基本驗證 即可使用

使用jcifs-1.3.17.jar(可到https://github.com/masconsult/android-ntlm 進行下載)

1 public class NTLMSchemeFactory implements AuthSchemeFactory2 {3     @Override4     public AuthScheme newInstance(HttpParams params)5     {6         return new NTLMScheme(new JCIFSEngine());7     }8 }
 1 public class JCIFSEngine implements NTLMEngine { 2  3     private static final int TYPE_1_FLAGS = NtlmFlags.NTLMSSP_NEGOTIATE_56 4             | NtlmFlags.NTLMSSP_NEGOTIATE_128 5             | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 6             | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN 7             | NtlmFlags.NTLMSSP_REQUEST_TARGET; 8  9     @Override10     public String generateType1Msg(String domain, String workstation)11             throws NTLMEngineException {12         final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS,13                 domain, workstation);14         return Base64.encode(type1Message.toByteArray());15     }16 17     @Override18     public String generateType3Msg(String username, String password,19             String domain, String workstation, String challenge)20             throws NTLMEngineException {21         Type2Message type2Message;22 23         try {24             type2Message = new Type2Message(Base64.decode(challenge));25         } catch (final IOException exception) {26             throw new NTLMEngineException("Error in type2 message", exception);27         }28 29         final int type2Flags = type2Message.getFlags();30         final int type3Flags = type2Flags31                 & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));32         final Type3Message type3Message = new Type3Message(type2Message,33                 password, domain, username, workstation, type3Flags);34         return Base64.encode(type3Message.toByteArray());35     }36 }

 

Android 與 IIS伺服器身分識別驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.