I'm trying to connect to our Snowflake DB using SSO with the ff connection string but keeps on getting an error. I'm using https://github.com/snowflakedb/snowflake-connector-net for my .NET 4.6.1 console application just to test things out. I have confirmed that my SSO is able to login to the Snowflake Web UI. Found related docs: https://docs.snowflake.com/en/user-guide/admin-security-fed-auth-use.html#native-sso-okta-only
connection string:
AUTHENTICATOR=https://{okta_account_name}.okta.com;ACCOUNT={org}.{region};HOST={org}.{region}.snowflakecomputing.com;PORT=443;ROLE={role};WAREHOUSE={warehouse};USER={sso email};password={sso password};DB={db};SCHEMA={schema}
error:
Exception: Error: Connection string is invalid: Unable to connect SqlState: 08006, VendorCode: 270008, QueryId:
Maybe I posted a question too soon. Following the Readme.md for Snowflake Dotnet Connector, I was able to generate the logs thru log4net. The underlying exception was
[2021-08-11 23:09:04,732] [1] [ERROR] [Snowflake.Data.Core.SFSession] Unable to connect
System.PlatformNotSupportedException: Operation is not supported on this platform.
at Snowflake.Data.Core.HttpUtil.setupCustomHttpHandler(HttpClientConfig config)
at Snowflake.Data.Core.HttpUtil.<>c__DisplayClass10_0.<RegisterNewHttpClientIfNecessary>b__1()
at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.<>c__DisplayClass5_0.<ConfigurePrimaryHttpMessageHandler>b__1(HttpMessageHandlerBuilder b)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass17_0.<CreateHandlerEntry>g__Configure|0(HttpMessageHandlerBuilder b)
at Microsoft.Extensions.Http.LoggingHttpMessageHandlerBuilderFilter.<>c__DisplayClass3_0.<Configure>b__0(HttpMessageHandlerBuilder builder)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(String name)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass14_0.<.ctor>b__1()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(String name)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name)
at Snowflake.Data.Core.HttpUtil.GetHttpClient(HttpClientConfig config)
at Snowflake.Data.Core.SFSession..ctor(String connectionString, SecureString password)
After browsing the web with similar exception it was mostly about the target .NET framework not satisfying the requirement. I was initially targeting .NET Framework 4.6.1 but after changing it to 4.7.2 everything worked fine as expected. It was indeed mentioned on the repo that the driver is now targeting 4.7.2 but I don't recall seeing it being unable to support lower framework versions. I tried downgrading the driver to the earliest version and upgrading it to the latest one by one with no avail. Hope this help others facing the same issue.
Based on my experience with Snowflake.Data, it supports .Net framework starting from version 4.6.1.
The connection string is like:
connStr="scheme=https;account=youraccount;host=xxx.snowflakecomputing.com;port=443;proxyhost=xx.myproxy.com;proxyport=80;role=MY_ROLE;warehouse=WAREHOUSE_NAME;user=userid;password=xxx;authenticator=xxx.okta.com"
You may also try to set authenticator=externalbrowser.
I personally prefer to connect to Snowflake using ODBC Driver. Please refer to https://docs.snowflake.com/en/user-guide/odbc.html on how to install ODBC driver in your local machine. Once you install and configure Snowflake in your machine, you may connect to Snowflake using System.Data.Odbclibrary.
The connection string might looks like:
connStr="Dsn=DataSourceName;uid=userID;pwd=xxx"
You may also try browser-based SSO:
In the connection string, set AUTHENTICATOR=externalbrowser, and set USER to the login name for your IdP.
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = "account=testaccount;authenticator=externalbrowser;user={login_name_for_IdP};db=testdb;schema=testschema";
conn.Open();
conn.Close();
}
where:
{login_name_for_IdP} is your login name for your IdP.
Related
I have simple connectionstring to MySql (MariaDB 5.5.5-10.11.0) written in c#:
MySqlConnection Database = new MySqlConnection("Server=127.0.0.1; Port=3306; Database=test; Uid=user; Pwd=MyPassword; Ssl Mode=Required; convert zero datetime=True;");
Everything works fine on two computers (Windows 10 and Windows 11). But when I try to launch this app on Windows Server 2022 I get this error:
System.InvalidCastException: Object cannot be cast from DBNull to other types.
at System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value, IFormatProvider provider)
at MySql.Data.MySqlClient.Driver.LoadCharacterSets(MySqlConnection connection)
at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MariaDB.Program.StartAPI()
Error is thrown on Database.Open();
MariaDB is installed and running, Ssl is working, user's pemissions are granted, port is correct. Any ideas please?
Whole program:
using System;
using MySql.Data.MySqlClient;
namespace MariaDB
{
internal class Program
{
MySqlConnection Database = new MySqlConnection("Server=127.0.0.1; Port=3306; Database=test; Uid=user; Pwd=MyPassword; Ssl Mode=Required; convert zero datetime=True;");
static void Main(string[] args)
{
Program p = new Program();
p.OpenDB();
}
private void OpenDB()
{
Database.Open();
Console.WriteLine("Ok");
Console.ReadLine();
}
}
}
The same thing happened to me.
Change MySql.Data.MySqlClient to MySqlConnector
and the problem was solved
The database I use is already in production so it was easier for me to change the connector instead of downgrading my database
I detail a little what I did:
My project was working on Visual Studio 2017 with .NET Core 2.1. I had to update to Visual Studio 2022 and change the .NET Core to version 3.1
In NuGet package manager uninstall MySql.Data and install MySqlConnector version 2.2.2
Follow the recommendations of the
official page MySqlConnector
This is caused by MariaDB 10.10.1 making ID field Nullable in Information_Schema.Collations and adding a bunch of Collations that have null for an ID.
https://jira.mariadb.org/browse/MDEV-27009
One possible workaround is to use MariaDB 10.9 or older.
I tried latest RC as well and it seems MariaDB will have this problem forever or atleast current MySql.Data.MySqlClient have a problem with it.
The suggested answer is either rolling back to 10.9 or using a totally different connector like MysqlConnector.
To switch to MysqlConnector, simply install that in your project via NuGet or if you want to build it yourself download it from Git.
Then in your app.config or web.config add the new dataProvider so .net knows about it.
ex, in app.config/web.config add:
<configuration><system.data><DbProviderFactories>
<remove invariant="MySqlConnector"/>
<add name="MySqlConnector" invariant="MySqlConnector" description="Async MySQL ADO.NET Connector" type="MySqlConnector.MySqlConnectorFactory, MySqlConnector, Culture=neutral" />
</DbProviderFactories>
then use it in your connectionStrings make sure you use dataProvider="MysqlConnector"
Special thanx to Ville & Miguel McFly!
Here is how the MySql.Data load collations:
This method assume that the "id" column's value is not null.
Unfortunately, from the version 10.10, MariaDB switch the "Id" column to "Allow Null" and there are a lot of row in Collations with NULL Id. That causes the "Object cannot be cast from DBNull to other types" exception.
Seem 10.9 is the only option for now if you wish to use the MySql.Data package and wait for a fix in MySql.Data.
I have a couple APIs and I'm in the process of changing the way I access my Azure SQL Databases from a simple credential to a Managed Service Identity model leveraging our Azure Active Directory.
I've had no problem doing this for my older .Net API but I cannot get my newer .NetCore API to do the same thing. I've read the possibility that .NetCore does not support this yet but it's not clear to me if this is accurate/true or not.
I run this code:
SqlConnection conn = new SqlConnection(_ConnectionString);
conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
and get this error:
AzureServiceTokenProviderException: Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried the following 3 methods to get an access token, but none of them worked.
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Managed Service Identity. Unable to connect to the Managed Service Identity (MSI) endpoint. Please check that you are running on an Azure resource that has MSI setup.
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Visual Studio. Access token could not be acquired. Visual Studio Token provider file not found at "C:\Users\simonb\AppData\Local\.IdentityService\AzureServiceAuth\tokenprovider.json"
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired.
I don't expect this to work through MSI because I'm running it locally but it should work through method 2
*update
Found this related article: https://github.com/Azure/azure-sdk-for-net/issues/3933
I've read the possibility that .NetCore does not support this yet but it's not clear to me if this is accurate/true or not.
In short, it not support in .Net Core 2.1.
This scenario is currently supported by .NET Framework 4.6 and above, but not by .NET Core 2.1. .NET Core 2.2 does support the scenario, but is not yet included in the default images in App Service.
For more details, you could refer to this article.
I am trying to open a connection to a Firebird database in my c# asp.net web application. It throws an error "The type initializer for 'FirebirdSql.Data.Common.TimeoutHelper' threw an exception."
I am new to Firebird.
In web.config I have:
<add name="FireBirdConnectionString" connectionString="Server=localhost;User=SYSDBA;Password=masterkey;Charser=NONE;Database=D:\data\SAMPLE.fdb" providerName="FirebirdSql.Data.FirebirdClient"/>
I have installed FirebirdSql.Data.FirebirdClient-4.5.1.0 and have added a namespace of
using FirebirdSql.Data.FirebirdClient;
I am opening the connection like below:
using (FbConnection con = new FbConnection(conString))
{
con.Open();
}
But this shows me an error as The type initializer for 'FirebirdSql.Data.Common.TimeoutHelper
What am I doing wrong?
I need to open the connection.
Even in connection string
and I have that path in my desktop too
Thank You..!
As I commented earlier, you are probably using the .NET 4.5 version in a .NET 4 project. You either need to switch your project to .NET 4.5 (which requires Visual Studio 2012 or 2013), or you need to replace the FirebirdSql.Data.FirebirdClient library with the .NET 4 version.
I'd suggest you use NuGet to install it, as this will verify - install time - which variant you need. Otherwise you need to pick the second link from http://www.firebirdsql.org/en/additional-downloads/ (iirc the first link only installs and includes the .NET 4.5 version).
I'm getting the following error over and over, despite all attempts to resolve the issue.
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
The main issue seems to connect to the EF over several projects in 1 solution.
Here's what I've already tried:
copied the connection string to the executing project in de web.config file
added references to the mysql providers
changed connection string names to the container name
Still get stuck at the construction of my ObjectContext where I get the error above.
public EcomContainer() :
base(#"name=ConString", "EcomContainer")
{
Configure();
}
I'm using the Devart mysql dotnet connector, EF6
My testunit is a regular C# console app which works fine with EF
but a new project with a web app such as a wep api or mvc application not.
If anyone would have some experience with this, I welcome any tips.
Thanks
I am relatively new to Visual Studio and I figured the best way to proceed would be to [sarcasm] do a major project [\sarcasm].
Summary of work to be done here:
VS2008 -> VS2012
Windows7 -> Windows8
SQL CompactServer v3.5 -> SQL CompactServer v4.0
.NET Framework v2.0 -> .NET Framework v4.0
One of the problems I am running into is that even though I have migrated my DB file in VS from SQL_CE_v3.5 to SQL_CE_v4.0 and updated my references to System.Data.SqlServerCe (4.0), I am getting the following exception when an attempt to open the db file is made the following way.
DB.DataContext = new DB.DataContext(CONNECTION_STRING);
Exception:
System.InvalidOperationException: Cannot open '|DataDirectory|\Database.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed.
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Initialize(IDataServices dataServices, Object connection)
at System.Data.Linq.DataContext.Init(Object connection, MappingSource mapping)
at System.Data.Linq.DataContext..ctor(String fileOrServerOrConnection, MappingSource mapping)
.
.
.<br>
Question:
How can I change the provider name here to 4.0 instead of 3.5?
What would be the best way to do all these migrations?
Initialze the DataContext object with a SqlCeConnection object, not a connection string, so something like:
DB.DataContext = new DB.DataContext(new SqlCeConnection(CONNECTION_STRING));