簡體   English   中英

cordova插件x509store

[英]cordova plugin x509store

我必須使用主要面向Windows平台的cordova開發一個應用程序。 在這個應用程序中,我必須操縱認證商店。 長話短說,我必須做一個cordova插件(或一個activeX技巧)。 我做在C#中的Windows運行時組件,如解釋在這里 ,使用的X509Store(因為我需要的Windows)。 我使用Visual Studio 2015制作了Windows運行時組件項目(我嘗試使用通用Windows和8.1)。 它可以工作,我可以在.js中調用C#方法。

但是問題是:Windows運行時組件項目沒有名稱空間System.Security.Cryptography.X509Certificates。 因此,我無法訪問X509Store。

作為一種解決方法,我制作了一個類庫(.NetCore,.dll),該類庫調用X509Store並返回字符串,至少顯示證書(json字符串化)。 經典類庫也可以訪問x509store,但是當我在此Windows運行時組件項目中引用它時,它將產生目標錯誤。 (便攜式/通用dll項目也沒有X509Store)。 我的.netcore dll有效,我在控制台應用程序(.NetCore)項目中嘗試了它,並顯示了我所有的證書。 但是,當我從cordova應用程序(cordova應用程序->插件->運行時-> .netcore dll)調用它時,它是空的(未找到證書,並且當前用戶未定義)。 我認為這是因為執行上下文不同(Web應用程序與控制台應用程序)。 而且我認為這不是一個很好的解決方案(甚至不起作用)。

因此,如何在Windows運行時組件中訪問(Windows用戶的)證書存儲? 由於我認為使用javascript不可能。

謝謝

PS:如果需要,我可以提供一些源代碼

編輯:
我忘記了在運行時項目中存在與.netcore dll的程序集沖突,這是通過在plugin.xml文件(System.Runtime.dll等)中引用正確的dll解決的,因為我沒有設法解決該問題。視覺工作室

//script inside cordova, called after device ready
signatureplugin.getAllCertNames(function(a) { }, function(a) { }, 0);

//signatureplugin.js
var exec = require('cordova/exec');

module.exports = {  
    getAllCertNames: function(successCallback, errorCallback, args) {
        exec(successCallback, errorCallback, "SignaturePlugin", "getAllCertNames", [args]);
    }
};

//signaturepluginProxy.js
module.exports = {  
    getAllCertNames: function(successCallback, errorCallback, args) {
        var res = SignerVerifier.PluginRT.getAllCertNames();
        for (var i = 0, len = res.length; i < len; i++) {
            alert(res[i]);
        }
    }
};

require("cordova/exec/proxy").add("SignaturePlugin", module.exports);


//windows runtime component
using CertStore;

namespace SignerVerifier
{
    public sealed class PluginRT
    {
        public static string[] getAllCertNames()
        {
            var certStore = new StoreManager();
            var res = certStore.getAllCertificates();
            return res;
        }

    }
}

//.NetCore dll
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using System;

namespace CertStore
{
    public class StoreManager
    {
        private X509Store store;

        public StoreManager()
        {
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        }

        public string[] getAllCertificates()
        {
            List<string> res = new List<string>();

            store.Open(OpenFlags.ReadOnly);
            var certificates = store.Certificates;
            foreach (var cert in certificates)
            {
                res.Add(JsonConvert.SerializeObject(cert));
            }
            store.Dispose();

            return res.ToArray();
        }
    }
}

如果我執行的是javascript空白應用程序項目+ Windows運行時組件項目(“通用”項目,則沒有“ Windows應用商店”,而我使用Windows 10),然后添加.netcore dll,我得到了導致異常的沖突:

System.IO.FileLoadException:無法加載文件或程序集'System.Runtime,版本= 4.1.0.0,區域性=中性,PublicKeyToken = b03f5f7f11d50a3a'或其依賴項之一。 找到的程序集的清單定義與程序集引用不匹配。 (來自HRESULT的異常:0x80131040)

(在cordova中,我通過.netcore中使用的nuget包NETStandard.Library.1.6.0引用System.Runtime.dll等來防止此異常),我必須錯過一些東西。 .NetCore dll似乎不兼容,但是Windows運行時項目的目標是.NetCore

編輯2 :(已解決)
vcsjones的答案=解決方法沒有用(並且以前的編輯沒有問題)。 但是在任何情況下都存在安全問題,我必須在appxmanifest的功能中檢查“共享用戶證書”

WinRT進行的證書管理與桌面和核心框架不同。 對於WinRT,您將使用Windows.Security命名空間。

您可以使用Windows.Security.Cryptography.Certificates.CertificateStore類打開和管理證書存儲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM