【问题标题】:cordova plugin x509store科尔多瓦插件 x509store
【发布时间】:2016-09-19 09:53:57
【问题描述】:

我必须用cordova 开发一个主要针对Windows 平台的应用程序。 在这个应用程序中,我必须操纵认证商店。长话短说,我必须做一个cordova插件(或者可能是一个activeX技巧)。 正如here 所解释的那样,我在 C# 中制作了一个 Windows 运行时组件,以使用 X509Store(因为我需要 Windows)。 我使用Visual Studio 2015 制作了一个Windows 运行时组件项目(我尝试了通用Windows 和8.1)。它有效,我可以在我的 .js 中调用 C# 方法。

但问题是:Windows 运行时组件项目没有命名空间 System.Security.Cryptography.X509Certificates。所以我无法访问 X509Store。

作为一种解决方法,我创建了一个类库(.NetCore、.dll),它调用 X509Store 并返回字符串,至少显示证书(json stringify)。一个经典的类库也可以访问 x509store,但是当我在这个 windows 运行时组件项目中引用它时,它会导致目标错误。 (便携式/通用 dll 项目也没有 X509Store)。我的 .netcore dll 有效,我在控制台应用程序 (.NetCore) 项目中尝试了它,它显示了我的所有证书。但是当我从cordova应用程序(cordova应用程序->插件->运行时->.netcore dll)调用它时,它是空的(没有找到证书,当前用户未定义)。我认为这是因为执行上下文不一样(webapp vs console app)。而且我认为这不是一个好的解决方案(甚至不起作用)。

那么,我如何在 Windows 运行时组件中访问(Windows 用户的)认证存储?因为我认为使用 javascript 是不可能的。

谢谢

P.S:如果需要,我可以提供一些源代码

编辑:
我忘记了运行时项目中存在与 .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 Store”并且我使用 Windows 10)然后添加 .netcore dll 我得到了导致的冲突例外:

System.IO.FileLoadException:无法加载文件或程序集“System.Runtime,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

(在cordova中,我通过引用.netcore中使用的nuget包NETStandard.Library.1.6.0中的System.Runtime.dll等来防止此异常) 我一定想念什么。 .NetCore dll好像不兼容,但是windows运行时项目的目标是.NetCore

编辑 2:(已解决)
vcsjones 的回答 = 无用的解决方法(之前的编辑没有问题)。 但无论如何都存在安全问题,我必须在 appxmanifest 的 Capabilities 中检查“共享用户证书”

【问题讨论】:

  • 能否提供代码示例。您使用个人商店还是机器商店?试试这个链接blogs.msdn.microsoft.com/stcheng/2013/03/12/… 尝试创建一个 Windows 商店应用程序,看看你的代码是否可以返回证书列表。我的猜测(因为您的代码只返回一个空列表并且没有例外)这是授权问题。您运行应用程序的用户无权访问证书/rsa 密钥存储。
  • 我编辑了我的帖子。我使用个人商店。如您所见,有一些 dll 冲突 => 异常。

标签: javascript c# windows cordova .net-core


【解决方案1】:

WinRT 执行不同于桌面和核心框架的证书管理。对于 WinRT,您将使用 Windows.Security 命名空间。

您可以使用Windows.Security.Cryptography.Certificates.CertificateStore 类打开和管理证书存储。

【讨论】:

  • 谢谢!我没有看到这门课,没有更多的“解决方法”
猜你喜欢
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2016-08-30
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 2017-12-11
相关资源
最近更新 更多