c# - Google API - Godaddy 上的服务帐户 C#

标签 c# google-api x509certificate2 service-accounts

我遇到了一个大问题。

我在 ASP.NET 中构建了一个 MVC WebAPI,它使用 google api(服务帐户)写入 google 电子表格。我可以很好地运行应用程序,也可以写入电子表格。但问题是当我将它托管在一个 godaddy 的共享 Windows 服务器上时,它给了我一个错误。


[CryptographicException:

An internal error occurred.

]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +33 System.Security.Cryptography.X509Certificates.X509Utils.

_LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)

+0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags) +218
System.Security.Cryptography.X509Certificates.X509Certificate.Import(String fileName, String password, X509KeyStorageFlags keyStorageFlags) +33
System.Security.Cryptography.X509Certificates.X509Certificate2.Import(String fileName, String password, X509KeyStorageFlags keyStorageFlags) +33
GAExampleMVC.GoogleAnalytics.GoogleAnalyticsService.GetStats() in d:\Aakash\Thermax\webapi\GAExampleMVC\GAExampleMVC\GoogleAnalytics\GoogleAnalytics.cs:50 GAExampleMVC.Controllers.HomeController.Index() in d:\Aakash\Thermax\webapi\GAExampleMVC\GAExampleMVC\Controllers\HomeController.cs:16 lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +211<br/> System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +253<br/> System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +189
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
System.Web.Mvc.Controller.ExecuteCore() +105


我只是不明白为什么它不能在 Godaddy 上运行,而同样的代码在本地主机上运行良好。

我想这与我的代码无法访问的某些证书​​存储有关。

任何人都可以帮助我了解确切的问题以及我该如何解决。

最佳答案

我知道这篇文章比较老,但我想我会根据我最近的经验提供一个答案,以防其他人遇到它。据我所知,实际问题是 GoDaddy 不允许更改共享主机上的应用程序池标识。要从根本上解决问题,必须为应用程序池标识设置“加载用户配置文件”设置。这允许加载 p12。但是,正如我所说,这不是共享托管平台的选择。所以,解决方法...

我的解决方案灵感来自 this post .我没有使用 Bouncy CaSTLe 的经验,也没有时间学习,所以这就是我的想法。

首先...获取 Google 使用开发人员控制台提供的 p12 文件并将其转换为 pk8 私钥。我使用 OpenSSL 来执行此操作。

openssl pkcs12 -in file.p12 -nocerts -out key.pem
openssl pkcs8 -in key.pem -topk8 -out p8key.pem

其次,将生成的 pk8 key 保存在您的应用程序 App_Data 文件夹中。

第三,使用 StreamReader 从 pk8 文件中读取 key ,并使用 .FromPrivateKey 而不是 .FromCertificate 初始化 ServiceAccountCredential。

Dim stReader As New System.IO.StreamReader _
(HostingEnvironment.ApplicationPhysicalPath & "[PathToPK8File].pk8")
        Dim aLine As String
        Dim keyfile As String
        While True
            aLine = stReader.ReadLine()
            If aLine Is Nothing Then
                keyfile = keyfile & vbCrLf
                Exit While
            Else
                keyfile = keyfile & aLine & " "
            End If
        End While

        Dim scopes As IList(Of String) = New List(Of String)()

        scopes.Add(CalendarService.Scope.CalendarReadonly)

        Dim credential As ServiceAccountCredential = New ServiceAccountCredential(
            New ServiceAccountCredential.Initializer(serviceEmail) With {
            .Scopes = scopes
            }.FromPrivateKey(keyfile)) // Use FromPrivateKey instead of FromCertificate

在生产服务器上正确加载所有内容。

关于c# - Google API - Godaddy 上的服务帐户 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28584148/

相关文章:

c# - 如何从 .NET 中的 X509Certificate2 中提取 AuthorityKeyIdentifier

c# - 为什么服务器可能收不到附加到使用 HttpClient 的请求的证书?

c# - UWP 中的鼠标和键盘 Hook : Raw input VS KeyEvents VS Global Hook

android - Google Developers Console 项目创建配额

安卓 Google+ 登录 "An internal error has occurred"

php - 如何使用 Google Calendar API v3/Google API 客户端库显示*所有*可用日历的列表?

c# - .NET Framework x509Certificate2 类,HasPrivateKey == true && PrivateKey == null?

c# - 以编程方式更改 CaSTLe Windsor 中的依赖项

c# - netduino 2 上的按钮事件

c# - 如何将可为空的 DateTime 转换为 UTC DateTime