问题描述:我目前的项目前端用的是vue2, 后端使用的是ABP,我的目的是想在前后端都进行证书的加载,
后端我是这样加载证书的,在StartUp.cs中添加以下的代码
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
.PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"c:\KeyFolder"))
.SetApplicationName("SharedAppName")
.ProtectKeysWithCertificate(GetCertificate());
}
// 获取证书
private X509Certificate2 GetCertificate()
{
var assembly = typeof(Startup).GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames().First(r => r.EndsWith("web.pfx"))))
{
try
{
Byte[] raw = new Byte[stream.Length];
for (Int32 i = 0; i < stream.Length; i++)
{
raw[i] = (Byte)stream.ReadByte();
}
X509Certificate2 cert = new X509Certificate2(raw, "34827729", X509KeyStorageFlags.MachineKeySet);
return cert;
}
catch (Exception e)
{
return null;
}
}
}
后端怎样添加证书呢,我百度了一下,大概是这样做的:
import axios from 'axios'
const path = require('path')
const fs = require('fs') //node.js的文件系统
const https = require('https')
//读取证书文件
var pfx = fs.readFileSync(path.join(__dirname, 'src/ssl/web.pfx')) //这一步报错,“不是有效的函数”
const cretPwd = '34827729'
//依据证书创建https agent
const agent = new https.Agent({
pfx,
passphrase: cretPwd
})
// 创建axios实例,里面使用的是htts agent(带证书)
const service = axios.create({
baseURL: window.configData.VUE_APP_BASE_API,
httpsAgent: agent
})
。。。
readFileSync处报错,提示“不是有效的函数”,我百度了一下,大概意思是说:
这里的fs是node.js的文件系统,只能适用于操作系统,不能用于浏览器中
换言之也就是要使用浏览器专用的文件系统才不会报错
我的问题:
1.怎样在vue中使用浏览器文件系统呢,果真能解决这个问题吗?
2.大家又是怎样在vue中加载证书的呢,恳请具体的代码片断或解决方案