dotConnect for Snowflake Documentation
On this page
    Authentication
    On this page

    dotConnect for Snowflake supports four authentication methods. To select a method, set the Authentication Type connection string parameter or the SnowflakeConnectionStringBuilder.AuthenticationType property.

    Authentication Type

    Description

    Required parameters

    BasicSigns in with the user name and a programmatic access token (PAT) or a password. This is the default method. In the connection dialogs, it is called Programmatic Access Token.User, Password
    KeyPairSigns in with an RSA key pair. The public key is assigned to the user in Snowflake, and the private key is passed in the connection string.User, Private Key
    OAuth2Signs in with an OAuth 2.0 refresh token issued by a Snowflake OAuth security integration.Refresh Token, Client Id, Client Secret
    OAuth2InteractiveSigns in as OAuth2. If Refresh Token is empty, the provider first opens a browser to sign in to Snowflake and gets the refresh token. See Interactive Sign-In.Client Id, Client Secret

    For all methods, you also need to specify the Domain parameter. We also recommend that you specify the Database parameter. The provider connects without it, but the connection dialog of Server Explorer in Visual Studio requires it. For more information, see Connecting to Snowflake.

    Basic Authentication (Programmatic Access Token)

    Specify the login name of the Snowflake user in the User parameter and a programmatic access token (PAT) of the user in the Password parameter. Snowflake accepts a programmatic access token in place of the password. A password works only for users who aren't required to use multi-factor authentication (MFA) when they sign in with a password. For information on how to generate a token, see Using programmatic access tokens for authentication in the Snowflake documentation.

    Domain=myorg-myaccount;Database=your_database;Warehouse=your_warehouse;User=your_user;Password=your_token;License Key=**********

    In the Add Connection dialog in Visual Studio, select Programmatic Access Token in the Authentication list, enter the user name, and enter the token in the Password (PAT) field.

    Add Connection dialog: Programmatic Access Token authentication

    For applications and services, use a programmatic access token, key-pair authentication, or OAuth authentication instead of a password.

    Key-Pair Authentication

    With key-pair authentication, dotConnect for Snowflake signs a JSON Web Token (JWT) with the private key of the user and sends it to Snowflake instead of a password. To use this method:

    1. Generate a private key. Run the following command to generate an unencrypted private key:
      openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8 -nocrypt
      To generate an encrypted private key, omit the -nocrypt option. OpenSSL prompts for a passphrase.
    2. Generate the public key from the private key:
      openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
    3. Assign the public key to the Snowflake user. Run the following statement in Snowflake, replacing the key value with the contents of rsa_key.pub without the -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- lines:
      ALTER USER your_user SET RSA_PUBLIC_KEY='MIIBIjANBgkqh...';
      Assigning a public key requires the SECURITYADMIN role or a role with the OWNERSHIP privilege on the user.
    4. In the connection string, set Authentication Type to KeyPair, specify the User, and pass the private key or the path to the private key file in the Private Key parameter. If the private key is encrypted, also specify the Passphrase parameter.

    The Private Key parameter takes either the private key itself or the path to the key file. If the value contains -----BEGIN, it is treated as the key; otherwise, it is treated as a path to an existing file. The key must be in the PEM format, including the -----BEGIN ... PRIVATE KEY----- and -----END ... PRIVATE KEY----- lines. Both PKCS#8 keys (BEGIN PRIVATE KEY, BEGIN ENCRYPTED PRIVATE KEY) and PKCS#1 keys (BEGIN RSA PRIVATE KEY) are supported. If you write the key in a connection string, enclose it in double quotes and keep its line breaks.

    In the Add Connection dialog in Visual Studio, select Key-pair in the Authentication list, enter the user name, and enter the private key or click the ellipsis (...) to select the key file. If the key is encrypted, also enter the passphrase.

    Add Connection dialog: Key-pair authentication

    Because the key spans several lines, it is easier to set it with the SnowflakeConnectionStringBuilder class than to write it in the connection string manually:

    SnowflakeConnectionStringBuilder builder = new SnowflakeConnectionStringBuilder();
    builder.Domain = "myorg-myaccount";
    builder.Database = "your_database";
    builder.Warehouse = "your_warehouse";
    builder.AuthenticationType = SnowflakeAuthenticationType.KeyPair;
    builder.User = "your_user";
    builder.PrivateKey = System.IO.File.ReadAllText(@"C:\keys\rsa_key.p8");
    // For an encrypted private key:
    // builder.Passphrase = "your_passphrase";
    
    using (SnowflakeConnection connection = new SnowflakeConnection(builder.ConnectionString)) {
      connection.Open();
    }
    
    Dim builder As New SnowflakeConnectionStringBuilder()
    builder.Domain = "myorg-myaccount"
    builder.Database = "your_database"
    builder.Warehouse = "your_warehouse"
    builder.AuthenticationType = SnowflakeAuthenticationType.KeyPair
    builder.User = "your_user"
    builder.PrivateKey = System.IO.File.ReadAllText("C:\keys\rsa_key.p8")
    ' For an encrypted private key:
    ' builder.Passphrase = "your_passphrase"
    
    Using connection As New SnowflakeConnection(builder.ConnectionString)
      connection.Open()
    End Using
    

    dotConnect for Snowflake takes the account name for the JWT from the Domain parameter: it is the part of the host name before the first dot. For example, for myorg-myaccount.snowflakecomputing.com, the account name is MYORG-MYACCOUNT.

    If the key cannot be read, the following exception is thrown: Could not read the private key. Check the passphrase and that Private Key is an RSA key in PEM format (PKCS#1 or PKCS#8, starting with -----BEGIN) or the path to such a file. Check that the key is complete, that it is in the PEM format, and that the passphrase is correct.

    If the value of Private Key doesn't contain -----BEGIN and there is no file at this path, the following exception is thrown: Private Key must be an RSA key in PEM format (starting with -----BEGIN) or the path to an existing key file; the file '<path>' was not found.

    OAuth 2.0 Authentication

    With OAuth 2.0 authentication, dotConnect for Snowflake exchanges a refresh token for an access token when it opens a connection and uses the access token to sign in. When the access token expires, the provider gets a new one automatically.

    To use this method, you need an OAuth client registered in Snowflake as a security integration, and a refresh token issued for this client:

    1. Create a security integration for a custom OAuth client. For example:
      CREATE SECURITY INTEGRATION my_oauth_client
        TYPE = OAUTH
        ENABLED = TRUE
        OAUTH_CLIENT = CUSTOM
        OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
        OAUTH_REDIRECT_URI = 'http://localhost:58088'
        OAUTH_ALLOW_NON_TLS_REDIRECT_URI = TRUE
        OAUTH_ISSUE_REFRESH_TOKENS = TRUE
        OAUTH_REFRESH_TOKEN_VALIDITY = 7776000;
      Creating a security integration requires the ACCOUNTADMIN role or a role with the CREATE INTEGRATION privilege.
    2. Get the client ID and client secret of the integration:
      SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('MY_OAUTH_CLIENT');
      The result contains the OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET values.
    3. Get a refresh token: let the provider sign in for you (see Interactive Sign-In) or use the OAuth 2.0 authorization code flow: open the authorization endpoint https://<account>.snowflakecomputing.com/oauth/authorize in a browser, sign in to Snowflake, and exchange the returned authorization code for tokens at the token endpoint https://<account>.snowflakecomputing.com/oauth/token-request. For more information, see the Snowflake documentation.
    4. In the connection string, set Authentication Type to OAuth2 and specify the Refresh Token, Client Id, and Client Secret parameters. To let the provider get the refresh token when the connection is opened, set Authentication Type to OAuth2Interactive instead and leave Refresh Token empty.
    Domain=myorg-myaccount;Database=your_database;Warehouse=your_warehouse;Authentication Type=OAuth2;Refresh Token=your_refresh_token;Client Id=your_client_id;Client Secret=your_client_secret;License Key=**********

    The refresh token is valid for the period set in the OAUTH_REFRESH_TOKEN_VALIDITY property of the integration. When the refresh token expires, get a new one.

    Interactive Sign-In

    If you set Authentication Type to OAuth2Interactive, leave Refresh Token empty, and open a connection in an application that can show a browser (not in a Windows service), dotConnect for Snowflake gets the refresh token itself:

    Domain=myorg-myaccount;Database=your_database;Warehouse=your_warehouse;Authentication Type=OAuth2Interactive;Client Id=your_client_id;Client Secret=your_client_secret;License Key=**********
    1. The provider opens the default browser at the authorization endpoint of your account.
    2. You sign in to Snowflake. The browser is redirected to http://localhost:58088, where the provider waits for the authorization code.
    3. The provider exchanges the code for tokens, puts the refresh token into the connection string of the connection, and continues to sign in as with any OAuth 2.0 connection. Authentication Type stays OAuth2Interactive.

    If Refresh Token is specified, an OAuth2Interactive connection signs in in the same way as an OAuth2 connection and doesn't open a browser. With Authentication Type set to OAuth2, the provider never opens a browser, and an empty Refresh Token causes an error.

    In a process that can't show a browser, an OAuth2Interactive connection with an empty Refresh Token throws the following exception: Refresh Token cannot be empty for the OAuth2Interactive authentication type in a non-interactive process: the sign-in to Snowflake needs a browser. Sign in once in an interactive application (the Connect dialog in Visual Studio or Entity Developer) and put the Refresh Token it gets into the connection string.

    In the Add/Modify Connection dialog in Visual Studio, the Authentication list shows both OAuth2 and OAuth2Interactive as OAuth 2.0. The Sign in to Snowflake button starts the same sign-in, fills out Refresh Token, and sets Authentication Type to OAuth2Interactive in the connection string.

    Add Connection dialog: OAuth 2.0 authentication

    The following requirements apply:

    Warehouse and Role

    For all authentication methods, you can specify the Warehouse and Role connection string parameters. If they are not set, Snowflake uses the default warehouse and the default role of the user. To execute queries, the session needs a running or auto-resumable warehouse and a role that has the USAGE privilege on this warehouse.

    Security Best Practices

    Follow these recommendations to secure Snowflake connections and protect authentication credentials:

    For help with authentication and connection issues, see Troubleshooting.

    See Also

    Connecting to Snowflake