|
| 1 | +--- |
| 2 | +author: xiaofanzhou |
| 3 | +ms.service: service-connector |
| 4 | +ms.topic: include |
| 5 | +ms.date: 07/17/2023 |
| 6 | +ms.author: xiaofanzhou |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +### [Java](#tab/java) |
| 12 | + |
| 13 | +1. Add the following dependencies in your *pom.xml* file: |
| 14 | + |
| 15 | + ```xml |
| 16 | + <dependency> |
| 17 | + <groupId>org.postgresql</groupId> |
| 18 | + <artifactId>postgresql</artifactId> |
| 19 | + <version>42.3.6</version> |
| 20 | + </dependency> |
| 21 | + <dependency> |
| 22 | + <groupId>com.azure</groupId> |
| 23 | + <artifactId>azure-identity-extensions</artifactId> |
| 24 | + <version>1.1.5</version> |
| 25 | + </dependency> |
| 26 | + ``` |
| 27 | + |
| 28 | + |
| 29 | +1. Get the connection string from environment variables and add the plugin name to connect to the database: |
| 30 | + |
| 31 | + ```java |
| 32 | + import java.sql.*; |
| 33 | + |
| 34 | + String url = System.getenv("AZURE_POSTGRESQL_CONNECTIONSTRING"); |
| 35 | + String pluginName = "com.Azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin"; |
| 36 | + Connection connection = DriverManager.getConnection(url + "&authenticationPluginClassName=" + pluginName); |
| 37 | + ``` |
| 38 | + |
| 39 | +For more information, see the following resources: |
| 40 | + |
| 41 | +* [Tutorial: Connect to PostgreSQL Database from a Java Quarkus Container App without secrets using a managed identity](../../container-apps/tutorial-java-quarkus-connect-managed-identity-postgresql-database.md) |
| 42 | +* [Tutorial: Connect to a PostgreSQL Database from Java Tomcat App Service without secrets using a managed identity](../../app-service/tutorial-java-tomcat-connect-managed-identity-postgresql-database.md) |
| 43 | +* [Quickstart: Use Java and JDBC with Azure Database for PostgreSQL Flexible Server](../../postgresql/flexible-server/connect-java.md?tabs=passwordless#connect-to-the-database) |
| 44 | + |
| 45 | +### [Spring](#tab/spring) |
| 46 | + |
| 47 | +For a Spring application, if you create a connection with option `--client-type springboot`, Service Connector will set the properties `spring.datasource.azure.passwordless-enabled`, `spring.datasource.url`, and `spring.datasource.username` to Azure Spring Apps. |
| 48 | + |
| 49 | +Update your application following the tutorial [Bind an Azure Database for PostgreSQL to your application in Azure Spring Apps](../../spring-apps/how-to-bind-postgres.md#prepare-your-java-project). Remember to remove the `spring.datasource.password` configuration property if it was set before and add the correct dependencies, |
| 50 | + |
| 51 | +For more tutorials, see [Use Spring Data JDBC with Azure Database for PostgreSQL](/azure/developer/java/spring-framework/configure-spring-data-jdbc-with-azure-postgresql?tabs=passwordless%2Cservice-connector&pivots=postgresql-passwordless-flexible-server#store-data-from-azure-database-for-postgresql) |
| 52 | + |
| 53 | +### [.NET](#tab/dotnet) |
| 54 | + |
| 55 | +For .NET, there's not a plugin or library for passwordless connections. You can get an access token for the managed identity or service principal and use it as the password to connect to the database. For example, you can use [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/) to get an access token for the managed identity or service principal: |
| 56 | + |
| 57 | +```csharp |
| 58 | +using Npgsql; |
| 59 | +using Azure.Identity; |
| 60 | +using Azure.Core; |
| 61 | + |
| 62 | +// user-assigned managed identity |
| 63 | +var sqlServerTokenProvider = new DefaultAzureCredential( |
| 64 | + new DefaultAzureCredentialOptions |
| 65 | + { |
| 66 | + ManagedIdentityClientId = userAssignedClientId |
| 67 | + }); |
| 68 | + |
| 69 | +// system-assigned managed identity |
| 70 | +//var sqlServerTokenProvider = new DefaultAzureCredential(); |
| 71 | + |
| 72 | +// service principal: tenantId, clientId, clientSecret can be retrieved from environment variables |
| 73 | +//var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret); |
| 74 | + |
| 75 | +AccessToken accessToken = await sqlServerTokenProvider.GetTokenAsync( |
| 76 | + new TokenRequestContext(scopes: new string[] |
| 77 | + { |
| 78 | + "https://ossrdbms-aad.database.windows.net/.default" |
| 79 | + })); |
| 80 | +string connectionString = |
| 81 | + $"{Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING")};Password={accessToken.Token}"; |
| 82 | + |
| 83 | +using (var connection = new NpgsqlConnection(connectionString)) |
| 84 | +{ |
| 85 | + Console.WriteLine("Opening connection using access token..."); |
| 86 | + connection.Open(); |
| 87 | + using var command = new NpgsqlCommand("SELECT version()", connection); |
| 88 | + using NpgsqlDataReader reader = await command.ExecuteReaderAsync(); |
| 89 | + |
| 90 | + while (reader.Read()) |
| 91 | + { |
| 92 | + Console.WriteLine("\nConnected!\n\nPostgreSQL version: {0}", reader.GetString(0)); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### [Others](#tab/others) |
| 98 | + |
| 99 | +For other languages, you can use the connection string and username that Service Connector set to the environment variables to connect the database. For environment variable details, see [Integrate Azure Database for PostgreSQL with Service Connector](../how-to-integrate-postgres.md). |
| 100 | + |
| 101 | +For more code samples, see [Connect to Azure databases from App Service without secrets using a managed identity](/azure/app-service/tutorial-connect-msi-azure-database?tabs=postgresql#3-modify-your-code). |
| 102 | + |
0 commit comments