中的运行时错误加载证书

中的运行时错误加载证书

本文介绍了Azure Functions 中的运行时错误加载证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将文件上传到 Office365 Sharepoint 文档库的 Azure 函数(C# API 通用 HTTP)方法.

I want to create an Azure function (C# API Generic HTTP) method that uploads a file to an Office365 Sharepoint document library.

因为 OneDrive API 允许我上传大文件(使用守护进程和证书身份验证),所以我使用 C# 控制台应用程序成功实现了目标.

Because OneDrive API allows me to upload large files (using daemon process & certificate authentication), I have succeeded in achieving the goal with a C# Console Application.

现在的想法是将代码移动到 Azure 函数中.但是,我在加载 pfx 证书的函数运行时收到错误消息.

The idea would be now to move the code into an Azure function.However, I receive an error during runtime of the function on the loading of the pfx-certificate.

public static async Task<bool> Run(HttpRequestMessage req, TraceWriter log)
{
   string certfile = System.IO.Path.Combine(Environment.ExpandEnvironmentVariable‌​s("%HOME%"), @"sitewwwroot<functionname>mykeyfile.pfx");

    X509Certificate2 cert = new X509Certificate2(certfile, "<myinsanepwd>");

    return true; //temporary
}

行 X509Certificate2 cert = new X509Certificate2(certfile, "");抛出异常System.Security.Cryptography.CryptographicException: 系统找不到指定的文件.

The line X509Certificate2 cert = new X509Certificate2(certfile, ""); throws an Exception System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

这真的很奇怪,因为文件存在于指定的路径上(我在方法中使用 File.Exists() 进行了检查:))这个错误可能与 support.microsoft.com/en-us/kb/948154 有关系吗?我该如何解决这个问题?

This is really strange because the file exists on the specified path (I checked using File.Exists() in the method :) )Could this error have something to do with support.microsoft.com/en-us/kb/948154 ? How can I solve this?

最好的问候,延斯

推荐答案

添加 X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet |X509KeyStorageFlags.Exportable 到构造函数.此代码适用于我:

Adding X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable to the constructor. This code works for me:

using System.Net;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    string certfile = System.IO.Path.Combine(Environment.ExpandEnvironmentVariable‌​s("%HOME%"), @"sitewwwrootHttpTriggerCSharp4myCertFile.pfx");
    log.Info(certfile);
    log.Info(System.IO.File.Exists(certfile).ToString());
    X509Certificate2 cert = new X509Certificate2(certfile, "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
    log.Info(cert.Thumbprint);

这篇关于Azure Functions 中的运行时错误加载证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:46