'Kerberos error when connecting to Oracle database in .NET Core

I have an application created using .NET Core 3.1 which needs to connect to an Oracle database, the connection code is very straightforward and I'm connecting using a username and password:

var con = new Oracle.ManagedDataAccess.Client.OracleConnection($"User Id={env.UserName};Password={env.UserPassword};Data Source={env.TNSName}")
conn.Open();

However the Open() fails with the following error:

{"NA  Kerberos5: 
Authentication handshake failure at stage: Could not load file or assembly 'Oracle.ManagedDataAccessIOP, Version=2.0.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342'. 
The system cannot find the file specified."}

Adding a reference to the Oracle.ManagedDataAccessIOP.dll (both 32 and 64 bit versions tried) does not resolve the error, it just changes it to the one shown below:

{"NA  Kerberos5: 
Authentication handshake failure at stage: 
Could not load file or assembly 'Oracle.ManagedDataAccessIOP, Version=2.0.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342'. 
An attempt was made to load a program with an incorrect format."}


Solution 1:[1]

The issue here is that the Managed ODP.NET package for .NET Core does not yet support Kerberos authentication and is unlikely to do so before .NET Core 5 when Microsoft will add the required Kerberos APIs to .NET Core. This is discussed on the Oracle site here.

You only get this error when the Oracle installation is configured to use Kerberos, usually by setting SQLNET.AUTHENTICATION_SERVICES to 'all' (or maybe'kerberos5'). When this is set to all kerberos5 is in the first tier of authentication methods to try so ODP.NET tries to load the Oracle.ManagedDataAccessIOP assembly that provides the interface to the Kerberos authentication and there isn't a version of this that will work with .NET Core.

The only solution I have come up with for this is to change the SQLNET.AUTHENTICATION_SERVICES value so that Kerberos is not required and one way to do this is to use the OracleConfiguration methods:

    OracleConfiguration.SqlNetAuthenticationServices = "none";
    var con = new Oracle.ManagedDataAccess.Client.OracleConnection($"User Id={env.UserName};Password={env.UserPassword};Data Source={env.TNSName}")
    conn.Open();

This then stops ODP.NET from trying to load the Oracle.ManagedDataAccessIOP.dll and the connection proceeds. This does mean that if you actually want Kerberos authentication in your .NET Core application you are out of luck until at least November 2020 and maybe longer as Microsoft have to provide the APIs and Oracle then have to build on top of them.

EDIT As of April 2022 this still hasn't been resolved and shows no sign of being resolved. Microsoft has closed the request to create the needed library and Oracle show no sign of wanting to pick this up. If you want Kerberos and Oracle then you can't use .NET Core.

Solution 2:[2]

I struggled with this error a lot, exact same code was working in all other developers machine and it was heavily used code in large application so adding OracleConfiguration.SqlNetAuthenticationServices was not a option.

I was able to fix this by updating sqlnet.ora file at c:\Program Files ()\Oracle_Admin\sqlnet.ora

Updated config

SQLNET.AUTHENTICATION_SERVICES=(NONE)

Thanks Jackson for pointing in correct direction.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Satish Gadekar