# InvalidOperationException: AESEncryptADONET in IBM.EntityFrameworkCore
###### tags: `ORM` `EF Core` `Entity Framework Core` `DB2` `AESEncryptADONET`
## ASP.NET Core MVC + Entity Framework Core for IBM DB2
在寫一個簡單的 ASP.NET Core MVC + Entity Framework Core for IBM DB2 的 Demo,要用在 .NET Core 3.1 的版本,在 NuGet 只看到居然沒有 DB2 阿!!

找了老半天終於看到 IBM 社群討論區有不少人在 !@#$%^&*,得自行去下載取得 Packages。

這裡有 IBM 整理好的 [IBM .NET Driver NuGet Packages for DB2 v11.5](https://epwt-www.mybluemix.net/software/support/trial/cst/programwebsite.wss?siteId=852&h=null&p=null&_ga=2.39363236.600670050.1596504966-1681514469.1594177913) 可以下載。
寫好後在本機啟動 IIS Express 測試皆 OK,但是...發佈到 IIS 上就發生 `InvalidOperationException: AESEncryptADONET`。
對了,我的環境如下:
* ASP.NET Core v3.1
* IBM .NET Core 3.1 / EntityFrameworkCore 3.1 in v11.5.4
* IIS 8.5
* Windows Server 2012 R2 (64 bits)
## InvalidOperationException: AESEncryptADONET
```
InvalidOperationException: AESEncryptADONET - Encryption - no error information available
IBM.Data.DB2.Core.DB2ConnPool.EncryptString(string value)
DB2Exception: ERROR [58005] [IBM][DB2.NET] SQL0902 An unexpected exception has occurred in Process: 2892 Thread 6 AppDomain: Name:asp_net_core_mvc_mysql
There are no context policies. Function: AESEncryptADONET (Encryption Info)
CallStack: at System.Environment.get_StackTrace()
at IBM.Data.DB2.Core.DB2ConnPool.HandleUnknownErrors(String strFncMsg, Exception exception, Boolean bThrow)
at IBM.Data.DB2.Core.DB2ConnPool.EncryptString(String value)
at IBM.Data.DB2.Core.DB2ConnPool.ReplaceConnectionStringParms(DB2Connection connection, String szValue, DB2ConnSettings& pSettings, DB2ConnSettingsInternal& pSettingsInternal, Boolean bAttach, Boolean pushDownStrAppended)
at IBM.Data.DB2.Core.DB2Connection.set_ConnectionString(String value)
at IBM.Data.DB2.Core.DB2Connection..ctor(String connectionString)
at IBM.EntityFrameworkCore.Storage.Internal.Db2SqlConnection.CreateDbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
...
```
好吧! 繼續 Google。
> Q: I am getting the following error:
>> An unexpected exception has occurred in Process: 244 Function: AESEncryptADONET (Encryption Info)
>
> The .NET driver is unable to find the compatible GSKit. Set the Path(on Windows) or LD_LIBRARY_PATH(on Linux) to the `<Package_Install_location>/<package-name>/<version>/build/clidriver/lib` directory of your environment. Remember, this may affect other applications.
>
> [name=[Frequently asked questions about IBM Db2 .NET Core Provider]]
原來要設定環境變數 **`Path`** 才可以,但是 ASP.NET Core 的系統環境變數要新增在 web.config 內。
1. 在 web.config,新增 `<environmentVariable name="Path" value="C:\Users\Administrator\.nuget\packages\ibm.data.db2.core\3.1.0.200\buildTransitive\clidriver\bin" />.`
```xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\[YOUR_APP].dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="Path" value="C:\Users\Administrator\.nuget\packages\ibm.data.db2.core\3.1.0.200\buildTransitive\clidriver\bin" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>
```
以及要存取 **`Path`** (`.nuget` 目錄) 的權限:
2. 設定目錄的安全性,新增 Application Pool Identity Accounts (`IIS AppPool\[App_Pool_Name]`) 允許 Read & Execute, Read, and List Folder Contents 的權限。
## References
* [DB2 .Net Connector error : AESEncryptADONet - Stack Overflow](https://stackoverflow.com/questions/63175964/db2-net-connector-error-aesencryptadonet)
* IBM Data Server Drivers:
- [Getting started with IBM .NET Provider for .NET Core](https://community.ibm.com/community/user/hybriddatamanagement/blogs/michelle-betbadal1/2020/04/29/getting-started-with-ibm-net-provider-for-net-core)
- [Db2 .NET Packages downloading and initial configuration](https://community.ibm.com/community/user/hybriddatamanagement/blogs/vishwa-hs1/2020/07/12/db2-net-packages-download-and-configure)
- [Frequently asked questions about IBM Db2 .NET Core Provider]
[Frequently asked questions about IBM Db2 .NET Core Provider]: https://community.ibm.com/community/user/hybriddatamanagement/blogs/michelle-betbadal1/2020/04/29/frequently-asked-questions-about-ibm-db-net-core-p