%3CLINGO-SUB%20id%3D%22lingo-sub-1517032%22%20slang%3D%22en-US%22%3EConnect%20from%20Function%20app%20with%20managed%20identity%20to%20Azure%20Database%20for%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1517032%22%20slang%3D%22en-US%22%3E%3CP%3EManaged%20identity%20is%20a%20feature%20that%20enables%20you%20to%20authenticate%20to%20Azure%20resources%20securely%20without%20needing%20to%20insert%20credentials%20into%20your%20code.%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManaged%20identities%3C%2FA%3E%26nbsp%3Bare%20automatically%20managed%20by%20Azure%20and%20enable%20you%20to%20authenticate%20to%20services%20that%20support%20Azure%20Active%20Directory%20authentication%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fconcepts-aad-authentication%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elike%20Azure%20Database%20for%20PostgreSQL%20%E2%80%93%20Single%20Server.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EToday%20we%E2%80%99ll%20create%20a%20managed%20identity%20for%20an%20Azure%20Function%20app%20and%20connect%20to%20an%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%26nbsp%3BDatabase%20for%20PostgreSQL%3C%2FA%3E%20server.%20(We%20also%20have%20a%20tutorial%20for%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fhowto-connect-with-managed-identity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Econnecting%20from%20a%20VM%20with%20managed%20identity%3C%2FA%3E).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20blog%2C%20we%E2%80%99ll%20be%20going%20through%20the%20following%20steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EStep%201%3A%26nbsp%3BConfigure%20Azure%20AD%20Authentication%20for%20PostgreSQL%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EStep%202%3A%20Enable%20managed%20identity%20for%20the%20Function%20app%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EStep%203%3A%20Use%20the%20managed%20identity%20ID%20to%20create%20a%20user%20in%20Postgres%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EStep%204%3A%20Write%20code%20for%20function%20app%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EStep%205%3A%20Test%20the%20function%20app%20and%20connect%20to%20Postgres%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1300816604%22%20id%3D%22toc-hId--1300816604%22%3EStep%201%3A%26nbsp%3B%20Configure%20Azure%20AD%20Authentication%20for%20Azure%20Database%20for%20PostgreSQL%20%E2%80%93%20Single%20Server%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%20need%20to%20make%20sure%20that%20our%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fquickstart-create-server-database-azure-cli%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%20server%3C%2FA%3E%20is%20configured%20for%20Azure%20Active%20Directory%20authentication.%20The%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fhowto-configure-sign-in-aad-authentication%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUse%20Azure%20Active%20Directory%20for%20authentication%20with%20PostgreSQL%3C%2FA%3E%20walkthrough%20shows%20you%20how%20to%20do%20so.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1186696229%22%20id%3D%22toc-hId-1186696229%22%3EStep%202%3A%20Enable%20a%20managed%20identity%20for%20the%20Function%20app%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20is%20to%20enable%20a%20system-assigned%20managed%20identify%20for%20the%20Azure%20Function%20app.%20A%20system-assigned%20managed%20identity%20is%20an%20Active%20Directory%20identity%20that%E2%80%99s%20created%20by%20Azure%20for%20a%20specific%20resource.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESign%20in%20to%20the%20Azure%20portal%20and%20select%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-create-first-azure-function%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20Function%20app%3C%2FA%3E%20you%E2%80%99d%20like%20to%20use.%20Select%20%3CSTRONG%3EIdentity%20%3C%2FSTRONG%3Eunder%20%3CSTRONG%3ESettings%3C%2FSTRONG%3E.%20In%20the%20%3CSTRONG%3ESystem%20assigned%3C%2FSTRONG%3E%20tab%2C%20set%20Status%20to%20%3CSTRONG%3EOn%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-2-Azure-Function-portal-page-Identity-tab.png%22%20style%3D%22width%3A%20796px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207309i63E75E15F7AA58A0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-2-Azure-Function-portal-page-Identity-tab.png%22%20alt%3D%22Step-2-Azure-Function-portal-page-Identity-tab.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--620758234%22%20id%3D%22toc-hId--620758234%22%3EStep%203%3A%20Use%20the%20managed%20identity%20ID%20to%20create%20a%20user%20in%20Postgres%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20will%20create%20a%20Postgres%20user%20for%20your%20managed%20identity.%20For%20this%20we%20need%20to%20get%20the%20application%E2%80%99s%20ID.%20Search%20and%20open%20%3CSTRONG%3EAzure%20Active%20Directory%3C%2FSTRONG%3E%20in%20the%20Azure%20portal.%20Select%20%3CSTRONG%3EEnterprise%20Applications%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-3-Enterprise-applications-portal-page.png%22%20style%3D%22width%3A%20983px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207330i27677AE1D04C68B7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-3-Enterprise-applications-portal-page.png%22%20alt%3D%22Step-3-Enterprise-applications-portal-page.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESelect%20your%20Function%20app%20and%20copy%20its%20%3CSTRONG%3EApplication%20ID%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-3b-Copy-Application-ID-from-portal.png%22%20style%3D%22width%3A%20718px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207311iCF769A6079950533%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-3b-Copy-Application-ID-from-portal.png%22%20alt%3D%22Step-3b-Copy-Application-ID-from-portal.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20connect%20to%20your%20Azure%20Database%20for%20PostgreSQL%20server%20using%20your%20Azure%20AD%20administrator%20user%20(from%20Step%201).%20Run%20the%20following%20in%20Postgres%2C%20substituting%20in%20your%20application%20ID%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESET%20aad_validate_oids_in_tenant%20%3D%20off%3B%0ACREATE%20ROLE%20myuser%20WITH%20LOGIN%20PASSWORD%20'%3CAPPLICATION_ID%3E'%20IN%20ROLE%20azure_ad_user%3B%3C%2FAPPLICATION_ID%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20managed%20identity%20now%20has%20access%20when%20authenticating%20to%20Postgres%20with%20the%20username%20%3CSTRONG%3Emyuser%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1866754599%22%20id%3D%22toc-hId-1866754599%22%3EStep%204%3A%20Write%20code%20for%20the%20Function%20app%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20let's%20see%20how%20to%20get%20an%20access%20token%20using%20the%20Function%20app%E2%80%99s%20system-managed%20identity.%20We%E2%80%99ll%20use%20that%20token%20to%20call%20Azure%20Database%20for%20PostgreSQL.%20Azure%20Database%20for%20PostgreSQL%20%E2%80%93%20Single%20Server%20natively%20supports%20Azure%20AD%20authentication%2C%20so%20it%20can%20directly%20accept%20access%20tokens%20obtained%20using%20managed%20identities%20for%20Azure%20resources.%20When%20creating%20a%20connection%20to%20PostgreSQL%2C%20you%20pass%20the%20access%20token%20in%20the%20password%20field.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere's%20a%20.NET%20code%20example%20of%20opening%20a%20connection%20to%20PostgreSQL%20using%20an%20access%20token.%20This%20code%20must%20run%20on%20the%20Function%20app%20to%20access%20the%20system-assigned%20managed%20identity's%20endpoint.%20.NET%20Framework%204.6%20or%20higher%20or%20.NET%20Core%202.2%20or%20higher%20is%20required%20to%20use%20the%20access%20token%20method.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EReplace%20the%20values%20of%20Servername%2C%20User%2C%20and%20Database%20to%20match%20yours.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System%3B%0Ausing%20System.IO%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20Microsoft.AspNetCore.Mvc%3B%0Ausing%20Microsoft.Azure.WebJobs%3B%0Ausing%20Microsoft.Azure.WebJobs.Extensions.Http%3B%0Ausing%20Microsoft.AspNetCore.Http%3B%0Ausing%20Microsoft.Extensions.Logging%3B%0Ausing%20Newtonsoft.Json%3B%0Ausing%20Npgsql%3B%0Ausing%20Microsoft.Azure.Services.AppAuthentication%3B%0Anamespace%20myfunctionappsudi%0A%7B%0A%20%20%20%20public%20static%20class%20Function1%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*Declare%20Database%20Details*%2F%0A%20%20%20%20%20%20%20%20private%20static%20string%20Host%20%3D%20%22%3CSERVERNAME%3E.postgres.database.azure.com%22%3B%2F*Database%20FQDN*%2F%0A%20%20%20%20%20%20%20%20private%20static%20string%20User%20%3D%20%22myuser%40%3CSERVERNAME%3E%22%3B%20%2F*User%20Created%20with%20managed%20identity*%2F%0A%20%20%20%20%20%20%20%20private%20static%20string%20DBname%20%3D%20%22%3CDB%20name%3D%22%22%3E%22%3B%2F*Database%20Name*%2F%0A%20%20%20%20%20%20%20%20private%20static%20string%20Port%20%3D%20%225432%22%3B%2F*Database%20Port*%2F%0A%20%20%20%20%20%20%20%20static%20string%20responsemsg%3B%0A%20%20%20%20%20%20%20%20%5BFunctionName(%22Function1%22)%5D%0A%20%20%20%20%20%20%20%20public%20static%20async%20Task%3CIACTIONRESULT%3E%20Run(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22get%22%2C%20%22post%22%2C%20Route%20%3D%20null)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(%22C%23%20HTTP%20trigger%20function%20processed%20a%20request.%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*Gettting%20the%20Token*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20sqlServerTokenProvider%20%3D%20new%20AzureServiceTokenProvider()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20SqlAccessToken%20%3D%20await%20sqlServerTokenProvider.GetAccessTokenAsync(%22https%3A%2F%2Fossrdbms-aad.database.windows.net%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Flog.LogInformation%20(SqlAccessToken)%3B%20%2F*For%20troubleshooting%20in%20case%20you%20need%20to%20print%20the%20token%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(%22Connecting%20to%20database.%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20connString%20%3D%20String.Format(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Server%3D%7B0%7D%3B%20User%20Id%3D%7B1%7D%3B%20Database%3D%7B2%7D%3B%20Port%3D%7B3%7D%3B%20Password%3D%7B4%7D%3BSSLMode%3DPrefer%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Host%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20User%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20DBname%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Port%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SqlAccessToken)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*Connecting%20to%20PostgreSQL*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20using%20(var%20conn%20%3D%20new%20NpgsqlConnection(connString))%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(%22Opening%20connection%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(connString)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.Open()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(%22Opening%20connection%20using%20access%20token....%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*Query%20the%20Database%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20using%20(var%20command%20%3D%20new%20NpgsqlCommand(%22SELECT%20version()%22%2C%20conn))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20reader%20%3D%20command.ExecuteReader()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(reader.Read())%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20log.LogInformation(%22%5CnConnected!%5Cn%5CnPostgres%20version%3A%20%7B0%7D%22%2C%20reader.GetString(0))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20responsemsg%20%3D%20reader.GetString(0)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20responsemsg%20%3D%20%22The%20version%20of%20the%20Azure%20Database%20for%20PostgreSQL%20is%20%3A%20%22%20%2B%20responsemsg%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(responsemsg)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FIACTIONRESULT%3E%3C%2FDB%3E%3C%2FSERVERNAME%3E%3C%2FSERVERNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-59300136%22%20id%3D%22toc-hId-59300136%22%3EStep%205%3A%20Test%20the%20Function%20app%20and%20connect%20to%20Postgres%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20publish%20the%20Function%20app%2C%20you%20can%20test%20it.%20In%20the%20Azure%20portal%2C%20go%20to%20the%20Function%20app%20you%20published%20and%20select%20%3CSTRONG%3EFunctions%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorRachel%20Agyemang_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-5-Function-app-portal-page.png%22%20style%3D%22width%3A%20796px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207323iB1DC898F529283B6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-5-Function-app-portal-page.png%22%20alt%3D%22Step-5-Function-app-portal-page.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20Function%20is%20selected%20you%20can%20choose%20%3CSTRONG%3ECode%2BTest%26nbsp%3B%3C%2FSTRONG%3Eand%20then%20%3CSTRONG%3ETest%2FRun%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-5b-Function%20Code-Test-portal-page.png%22%20style%3D%22width%3A%20862px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207313i38F84FFF4B898E95%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-5b-Function%20Code-Test-portal-page.png%22%20alt%3D%22Step-5b-Function%20Code-Test-portal-page.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESelect%20%3CSTRONG%3ERun%3C%2FSTRONG%3E%20in%20the%20pop-up%20window.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorRachel%20Agyemang_5%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-5c-Run-function.png%22%20style%3D%22width%3A%20491px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207324i2E2AC9D30B77C7E2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-5c-Run-function.png%22%20alt%3D%22Step-5c-Run-function.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20see%20the%20function%E2%80%99s%20output%20in%20terminal%20for%20App%20Insights.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorRachel%20Agyemang_6%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Step-5d-Terminal-output-for-App-Insights.png%22%20style%3D%22width%3A%20898px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207325iF10CE6409FBBE339%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Step-5d-Terminal-output-for-App-Insights.png%22%20alt%3D%22Step-5d-Terminal-output-for-App-Insights.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20can%20see%20that%20the%20function%20connected%20to%20the%20Postgres%20database%20with%20managed%20identity%20and%20could%20successfully%20run%20the%20query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1748154327%22%20id%3D%22toc-hId--1748154327%22%3EYou%20no%20longer%20have%20to%20keep%20credentials%20in%20code%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EManaged%20identity%20sets%20you%20free%20from%20storing%20credentials%20in%20code%20or%20source%20control.%20If%20you%E2%80%99re%20interested%20in%20how%20to%20use%20managed%20identity%20to%20connect%20from%20an%20Azure%20VM%20to%20Azure%20Database%20for%20PostgreSQL%20-%20Single%20Server%2C%20check%20out%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fhowto-connect-with-managed-identity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eour%20walkthrough%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1517032%22%20slang%3D%22en-US%22%3E%3CP%3EDon't%20keep%20credentials%20in%20your%20code%20-%20use%20a%20managed%20identity%20instead%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22teaser-function.md.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F207328i5A49650B014CA5C8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22teaser-function.md.png%22%20alt%3D%22teaser-function.md.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1517032%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EActive%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAuthentication%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFunction%20App%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFunctions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EManaged%20Identity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Managed identity is a feature that enables you to authenticate to Azure resources securely without needing to insert credentials into your code. Managed identities are automatically managed by Azure and enable you to authenticate to services that support Azure Active Directory authentication, like Azure Database for PostgreSQL – Single Server.

 

Today we’ll create a managed identity for an Azure Function app and connect to an Azure Database for PostgreSQL server. (We also have a tutorial for connecting from a VM with managed identity).

 

In this blog, we’ll be going through the following steps:

  • Step 1: Configure Azure AD Authentication for PostgreSQL
  • Step 2: Enable managed identity for the Function app
  • Step 3: Use the managed identity ID to create a user in Postgres
  • Step 4: Write code for function app
  • Step 5: Test the function app and connect to Postgres

 

Step 1:  Configure Azure AD Authentication for Azure Database for PostgreSQL – Single Server

 

First, we need to make sure that our Azure Database for PostgreSQL server is configured for Azure Active Directory authentication. The Use Azure Active Directory for authentication with PostgreSQL walkthrough shows you how to do so.

 

 

Step 2: Enable a managed identity for the Function app

 

Next is to enable a system-assigned managed identify for the Azure Function app. A system-assigned managed identity is an Active Directory identity that’s created by Azure for a specific resource.

 

Sign in to the Azure portal and select the Function app you’d like to use. Select Identity under Settings. In the System assigned tab, set Status to On.

 

Step-2-Azure-Function-portal-page-Identity-tab.png

 

 

Step 3: Use the managed identity ID to create a user in Postgres

 

Now we will create a Postgres user for your managed identity. For this we need to get the application’s ID. Search and open Azure Active Directory in the Azure portal. Select Enterprise Applications.

 

Step-3-Enterprise-applications-portal-page.png

 

Select your Function app and copy its Application ID.

 

Step-3b-Copy-Application-ID-from-portal.png

 

Now, connect to your Azure Database for PostgreSQL server using your Azure AD administrator user (from Step 1). Run the following in Postgres, substituting in your application ID:

 

SET aad_validate_oids_in_tenant = off;
CREATE ROLE myuser WITH LOGIN PASSWORD '<APPLICATION_ID>' IN ROLE azure_ad_user;

 

The managed identity now has access when authenticating to Postgres with the username myuser.

 

 

Step 4: Write code for the Function app

 

Next let's see how to get an access token using the Function app’s system-managed identity. We’ll use that token to call Azure Database for PostgreSQL. Azure Database for PostgreSQL – Single Server natively supports Azure AD authentication, so it can directly accept access tokens obtained using managed identities for Azure resources. When creating a connection to PostgreSQL, you pass the access token in the password field.

 

Here's a .NET code example of opening a connection to PostgreSQL using an access token. This code must run on the Function app to access the system-assigned managed identity's endpoint. .NET Framework 4.6 or higher or .NET Core 2.2 or higher is required to use the access token method.

 

Replace the values of Servername, User, and Database to match yours.

 

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using Microsoft.Azure.Services.AppAuthentication;
namespace myfunctionappsudi
{
    public static class Function1
    {
        /*Declare Database Details*/
        private static string Host = "<Servername>.postgres.database.azure.com";/*Database FQDN*/
        private static string User = "myuser@<Servername>"; /*User Created with managed identity*/
        private static string DBname = "<DB Name>";/*Database Name*/
        private static string Port = "5432";/*Database Port*/
        static string responsemsg;
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            /*Gettting the Token*/
            var sqlServerTokenProvider = new AzureServiceTokenProvider();
            var SqlAccessToken = await sqlServerTokenProvider.GetAccessTokenAsync("https://ossrdbms-aad.database.windows.net");
            //log.LogInformation (SqlAccessToken); /*For troubleshooting in case you need to print the token */
            log.LogInformation("Connecting to database.");
            string connString = String.Format(
                    "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
                    Host,
                    User,
                    DBname,
                    Port,
                    SqlAccessToken);
            /*Connecting to PostgreSQL*/
            using (var conn = new NpgsqlConnection(connString))
            {
                log.LogInformation("Opening connection");
                log.LogInformation(connString);
                conn.Open();
                log.LogInformation("Opening connection using access token....");
                /*Query the Database */
                    using (var command = new NpgsqlCommand("SELECT version()", conn))
                    {

                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            log.LogInformation("\nConnected!\n\nPostgres version: {0}", reader.GetString(0));
                          responsemsg = reader.GetString(0);
                        }
                    }
              }
             responsemsg = "The version of the Azure Database for PostgreSQL is : " + responsemsg;
            return new OkObjectResult(responsemsg);
        }
    }
}

 

 

 

 

Step 5: Test the Function app and connect to Postgres

 

Once you publish the Function app, you can test it. In the Azure portal, go to the Function app you published and select Functions.

 

Step-5-Function-app-portal-page.png

 

Once the Function is selected you can choose Code+Test and then Test/Run

 

 Step-5b-Function Code-Test-portal-page.png

 

Select Run in the pop-up window.

 

Step-5c-Run-function.png

 

You can see the function’s output in terminal for App Insights.

 

Step-5d-Terminal-output-for-App-Insights.png

 

We can see that the function connected to the Postgres database with managed identity and could successfully run the query.

 

 

You no longer have to keep credentials in code

 

Managed identity sets you free from storing credentials in code or source control. If you’re interested in how to use managed identity to connect from an Azure VM to Azure Database for PostgreSQL - Single Server, check out our walkthrough.