首页新闻找找看学习计划

请教一个 C# https双向认证的例子

0
悬赏园豆:5 [已关闭问题] 关闭于 2017-03-23 15:48

有java 代码 现在要转换成C# 代码 ,主要是第二段代码改如何转换 最好有核心代码列举一下,不胜感激

public class Authentication
{   
    @SuppressWarnings("resource")
    public static void main(String args[]) throws Exception
    {
        
        HttpsUtil httpUtils = new HttpsUtil();
        httpUtils.initSSLConfigForTwoWay();  //Two-Way Authentication
        
        String appId = "c5999872-f6e0-4663-8946-c2ff8b2baae0";  //please replace the appId, when you use the demo.
        String secret = "875eadb27b0fc4df5a6f";    //please replace the secret, when you use the demo.
        String url = "https://******";  //please replace the IP and Port, when you use the demo.
        
        Map<String, String> param = new HashMap<String, String>();
        param.put("appId", appId);
        param.put("secret", secret);
        
        String body =  httpUtils.doPostFormUrlEncodedForString(url, param);        

        System.out.println(body);
    }


HttpsUtil 
public void initSSLConfigForTwoWay() throws Exception {
        // 1 Import your own certificate
        KeyStore selfCert = KeyStore.getInstance("pkcs12");
        selfCert.load(new FileInputStream(SELFCERTPATH),
                SELFCERTPWD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
        kmf.init(selfCert, SELFCERTPWD.toCharArray());

        // 2 Import the CA certificate of the server,
        KeyStore caCert = KeyStore.getInstance("jks");
        caCert.load(new FileInputStream(TRUSTCAPATH), TRUSTCAPWD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
        tmf.init(caCert);

        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        // 3 Set the domain name to not verify
        // (Non-commercial IoT platform, no use domain name access generally.)
        SSLSocketFactory ssf = new SSLSocketFactory(sc,
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        //If the platform has already applied for a domain name which matches the domain name in the certificate information, the certificate 
        //domain name check can be enabled (open by default)
        // SSLSocketFactory ssf = new SSLSocketFactory(sc);

        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 8743, ssf));
        
        httpClient = new DefaultHttpClient(ccm);
    }

 


zklve2的主页 zklve2 | 初学一级 | 园豆:6
提问于:2017-03-02 10:54
< >
分享
所有回答(1)
1

c#官方的代码是不能通过的,基本上写法和java一致才能通过(跳过)验证,照到上面改就行了,就是一些大小写及个别单词不一样。

花飘水流兮 | 园豆:10601 (专家六级) | 2017-03-02 13:11

能不能把主要用到的几个类型 列举一下 步骤肯定是跟着写没问题,但是现在问题是对应不上C#中的类

支持(0) 反对(0) zklve2 | 园豆:6 (初学一级) | 2017-03-02 13:28

@zklveyjx: 怎么可能,原本就是invoke java。

支持(0) 反对(0) 花飘水流兮 | 园豆:10601 (专家六级) | 2017-03-02 13:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册