%3CLINGO-SUB%20id%3D%22lingo-sub-1288248%22%20slang%3D%22en-US%22%3EUsing%20Managed%20Service%20Identity%20(MSI)%20to%20authenticate%20on%20Azure%20SQL%20DB%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1288248%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20all%20know%20that%20we%20can%20use%20%3CSTRONG%3ESQL%20authentication%3C%2FSTRONG%3E%20or%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-aad-authentication-configure%3Ftabs%3Dazure-powershell%23create-contained-database-users-in-your-database-mapped-to-azure-ad-identities%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20AD%20authentication%3C%2FA%3E%3C%2FSTRONG%3E%20to%20log%20on%20Azure%20SQL%20DB.%20We%20can%20also%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-aad-authentication-configure%3Ftabs%3Dazure-powershell%23azure-ad-token%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20AD%20Token%20authentication%3C%2FA%3E%26nbsp%3Bor%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-aad-authentication-configure%3Ftabs%3Dazure-powershell%23azure-ad-token%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecertificate-based%20authentication%3C%2FA%3E%2C%20but%20we%20will%20not%20explore%20these%20ones%20here.%26nbsp%3B%3CSTRONG%3EIn%20this%20article%20we%20will%20explore%20Managed%20Service%20Identity%20(MSI)%20authentication%20or%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Ftutorial-windows-vm-access-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esystem-assigned%20identity%3C%2FA%3E%3C%2FSTRONG%3E%2C%20and%20how%20to%20use%20it%20on%20Azure%20VM%20(Using%20Powershell)%20or%20on%20an%20Azure%20Function%20(.NET).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1118526144%22%20id%3D%22toc-hId-1118526144%22%3E%3CSPAN%3EUnderstanding%20Managed%20Identity%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EThere%20are%20two%20types%20of%20managed%20identities%3A%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EA%26nbsp%3B%3CSTRONG%3Esystem-assigned%20managed%20identity%3C%2FSTRONG%3E%26nbsp%3Bis%20enabled%20directly%20on%20an%20Azure%20service%20instance.%20When%20the%20identity%20is%20enabled%2C%20Azure%20creates%20an%20identity%20for%20the%20instance%20in%20the%20Azure%20AD%20tenant%20that's%20trusted%20by%20the%20subscription%20of%20the%20instance.%20After%20the%20identity%20is%20created%2C%20the%20credentials%20are%20provisioned%20onto%20the%20instance.%20The%20lifecycle%20of%20a%20system-assigned%20identity%20is%20directly%20tied%20to%20the%20Azure%20service%20instance%20that%20it's%20enabled%20on.%20If%20the%20instance%20is%20deleted%2C%20Azure%20automatically%20cleans%20up%20the%20credentials%20and%20the%20identity%20in%20Azure%20AD.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EA%26nbsp%3B%3CSTRONG%3Euser-assigned%20managed%20identity%3C%2FSTRONG%3E%26nbsp%3Bis%20created%20as%20a%20standalone%20Azure%20resource.%20Through%20a%20create%20process%2C%20Azure%20creates%20an%20identity%20in%20the%20Azure%20AD%20tenant%20that's%20trusted%20by%20the%20subscription%20in%20use.%20After%20the%20identity%20is%20created%2C%20the%20identity%20can%20be%20assigned%20to%20one%20or%20more%20Azure%20service%20instances.%20The%20lifecycle%20of%20a%20user-assigned%20identity%20is%20managed%20separately%20from%20the%20lifecycle%20of%20the%20Azure%20service%20instances%20to%20which%20it's%20assigned.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3E%3CSTRONG%3EInternally%2C%20managed%20identities%20are%20service%20principals%20of%20a%20special%20type%2C%20which%20are%20locked%20to%20only%20be%20used%20with%20Azure%20resources.%20When%20the%20managed%20identity%20is%20deleted%2C%20the%20corresponding%20service%20principal%20is%20automatically%20removed.%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EYour%20code%20can%20use%20a%20managed%20identity%20to%20request%20access%20tokens%20for%20services%20that%20support%20Azure%20AD%20authentication.%20Azure%20takes%20care%20of%20rolling%20the%20credentials%20that%20are%20used%20by%20the%20service%20instance.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EThe%20following%20diagram%20shows%20how%20managed%20service%20identities%20work%20with%20Azure%20virtual%20machines%20(VMs)%3A%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22data-flow.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%2F182737i4B1387A5E58E6C2D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22data-flow.png%22%20alt%3D%22data-flow.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20id%3D%22how-a-system-assigned-managed-identity-works-with-an-azure-vm%22%20id%3D%22toc-hId--688928319%22%20id%3D%22toc-hId--688928319%22%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EHow%20a%20system-assigned%20managed%20identity%20works%20with%20an%20Azure%20VM%3C%2FEM%3E%3C%2FFONT%3E%3C%2FH3%3E%0A%3COL%3E%0A%3CLI%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EAzure%20Resource%20Manager%20receives%20a%20request%20to%20enable%20the%20system-assigned%20managed%20identity%20on%20a%20VM.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EAzure%20Resource%20Manager%20creates%20a%20service%20principal%20in%20Azure%20AD%20for%20the%20identity%20of%20the%20VM.%20The%20service%20principal%20is%20created%20in%20the%20Azure%20AD%20tenant%20that's%20trusted%20by%20the%20subscription.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EAzure%20Resource%20Manager%20configures%20the%20identity%20on%20the%20VM%20by%20updating%20the%20Azure%20Instance%20Metadata%20Service%20identity%20endpoint%20with%20the%20service%20principal%20client%20ID%20and%20certificate.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EAfter%20the%20VM%20has%20an%20identity%2C%20use%20the%20service%20principal%20information%20to%20grant%20the%20VM%20access%20to%20Azure%20resources.%20To%20call%20Azure%20Resource%20Manager%2C%20use%20role-based%20access%20control%20(RBAC)%20in%20Azure%20AD%20to%20assign%20the%20appropriate%20role%20to%20the%20VM%20service%20principal.%20To%20call%20Key%20Vault%2C%20grant%20your%20code%20access%20to%20the%20specific%20secret%20or%20key%20in%20Key%20Vault.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EYour%20code%20that's%20running%20on%20the%20VM%20can%20request%20a%20token%20from%20the%20Azure%20Instance%20Metadata%20service%20endpoint%2C%20accessible%20only%20from%20within%20the%20VM%3A%26nbsp%3B%3CCODE%3E%3CA%20href%3D%22http%3A%2F%2F169.254.169.254%2Fmetadata%2Fidentity%2Foauth2%2Ftoken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2F169.254.169.254%2Fmetadata%2Fidentity%2Foauth2%2Ftoken%3C%2FA%3E%3C%2FCODE%3E%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EThe%20resource%20parameter%20specifies%20the%20service%20to%20which%20the%20token%20is%20sent.%20To%20authenticate%20to%20Azure%20Resource%20Manager%2C%20use%26nbsp%3B%3CCODE%3Eresource%3D%3CA%20href%3D%22https%3A%2F%2Fmanagement.azure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmanagement.azure.com%2F%3C%2FA%3E%3C%2FCODE%3E.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EAPI%20version%20parameter%20specifies%20the%20IMDS%20version%2C%20use%20api-version%3D2018-02-01%20or%20greater.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EA%20call%20is%20made%20to%20Azure%20AD%20to%20request%20an%20access%20token%20(as%20specified%20in%20step%205)%20by%20using%20the%20client%20ID%20and%20certificate%20configured%20in%20step%203.%20Azure%20AD%20returns%20a%20JSON%20Web%20Token%20(JWT)%20access%20token.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EYour%20code%20sends%20the%20access%20token%20on%20a%20call%20to%20a%20service%20that%20supports%20Azure%20AD%20authentication.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH3%20id%3D%22toc-hId-1798584514%22%20id%3D%22toc-hId-1798584514%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22how-a-user-assigned-managed-identity-works-with-an-azure-vm%22%20id%3D%22toc-hId--8869949%22%20id%3D%22toc-hId--8869949%22%3EHands%20On%3C%2FH3%3E%0A%3CP%3ETo%20assign%20an%20Identity%20to%20an%20Azure%20VM%20you%20can%20just%20go%20to%20Azure%20Portal%20and%20enable%20it%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22FonsecaSergio_0-1586264008652.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%2F182738i1D8D32CABE186A7E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22FonsecaSergio_0-1586264008652.png%22%20alt%3D%22FonsecaSergio_0-1586264008652.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20will%20be%20able%20to%20find%20this%20identity%20on%20%3CSTRONG%3EAzure%20Active%20Directory%20%26gt%3B%20Enterprise%20applications%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-04-07%2015_10_12-Clipboard.png%22%20style%3D%22width%3A%20242px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F182759i80EDC225033E1EBE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-04-07%2015_10_12-Clipboard.png%22%20alt%3D%222020-04-07%2015_10_12-Clipboard.png%22%20%2F%3E%3C%2FSPAN%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%222020-04-07%2015_12_00-Enterprise%20applications%20_%20All%20applications%20-%20Microsoft%20Azure.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%2F182758i7BBCDFB6161C81CD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-04-07%2015_12_00-Enterprise%20applications%20_%20All%20applications%20-%20Microsoft%20Azure.png%22%20alt%3D%222020-04-07%2015_12_00-Enterprise%20applications%20_%20All%20applications%20-%20Microsoft%20Azure.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%3CP%3EYou%20can%20also%20get%20and%20Identity%20on%20an%20%3CSTRONG%3EAzure%20Webapp%20or%20Azure%20Function%3C%2FSTRONG%3E%20like%20sample%20below%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-04-07%2014_00_51-Clipboard.png%22%20style%3D%22width%3A%20872px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F182744iE6E928555A79D495%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-04-07%2014_00_51-Clipboard.png%22%20alt%3D%222020-04-07%2014_00_51-Clipboard.png%22%20%2F%3E%3C%2FSPAN%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%222020-04-07%2014_04_09-Edit%20Article%20-%20Microsoft%20Tech%20Community.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%2F182742i9AAC953A9BD6CA46%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-04-07%2014_04_09-Edit%20Article%20-%20Microsoft%20Tech%20Community.png%22%20alt%3D%222020-04-07%2014_04_09-Edit%20Article%20-%20Microsoft%20Tech%20Community.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1816324412%22%20id%3D%22toc-hId--1816324412%22%3EGiving%20SQL%20Permission%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3EFirst%20make%20sure%20your%20Azure%20SQL%20DB%20have%20an%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-aad-authentication-configure%3Ftabs%3Dazure-powershell%23provision-an-azure-active-directory-administrator-for-your-azure-sql-database-server%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAAD%20Admin%20provisioned%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EGo%20to%20database%20and%20add%20it%20like%20adding%20a%20regular%20AAD%20user%2C%20using%20name%20of%20the%20identity%20(VM%20or%20name%20of%20function)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EDROP%20USER%20IF%20EXISTS%20%5BVMSQLCLient%5D%3CBR%20%2F%3EGO%3CBR%20%2F%3ECREATE%20USER%20%5B%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EVMSQLCLient%3C%2FSTRONG%3E%3C%2FFONT%3E%5D%20FROM%20%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EEXTERNAL%20PROVIDER%3C%2FSTRONG%3E%3C%2FFONT%3E%3B%3CBR%20%2F%3EGO%3CBR%20%2F%3EALTER%20ROLE%20db_datareader%20ADD%20MEMBER%20%5BVMSQLCLient%5D%3B%3CBR%20%2F%3EALTER%20ROLE%20db_datawriter%20ADD%20MEMBER%20%5BVMSQLCLient%5D%3B%3CBR%20%2F%3EGRANT%20EXECUTE%20TO%20%5BVMSQLCLient%5D%3CBR%20%2F%3EGO%3CBR%20%2F%3E---------------------------------------------------%3CBR%20%2F%3EDROP%20USER%20IF%20EXISTS%20%5Btest123fonsecanet%5D%3CBR%20%2F%3EGO%3CBR%20%2F%3ECREATE%20USER%20%5B%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3Etest123fonsecanet%3C%2FSTRONG%3E%3C%2FFONT%3E%5D%20FROM%20%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EEXTERNAL%20PROVIDER%3C%2FSTRONG%3E%3C%2FFONT%3E%3B%3CBR%20%2F%3EGO%3CBR%20%2F%3EALTER%20ROLE%20db_datareader%20ADD%20MEMBER%20%5Btest123fonsecanet%5D%3B%3CBR%20%2F%3EALTER%20ROLE%20db_datawriter%20ADD%20MEMBER%20%5Btest123fonsecanet%5D%3B%3CBR%20%2F%3EGRANT%20EXECUTE%20TO%20%5Btest123fonsecanet%5D%3CBR%20%2F%3EGO%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-671188421%22%20id%3D%22toc-hId-671188421%22%3EScenario%201%3A%20Using%20Azure%20VM%20-%20Powershell%20test%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3EAfter%20assigning%20MSI%20to%20VM%2C%20and%20granting%20the%20required%20permissions%20on%20SQL.%20Start%20the%20VM%3C%2FLI%3E%0A%3CLI%3EOpen%20Powershell%20ISE%3C%2FLI%3E%0A%3CLI%3ERun%20a%20test%20script%20like%20sample%20below%0A%3CUL%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FFonsecaSergio%2FScriptCollection%2Fblob%2Fmaster%2FPowershell%2FConnect%2520to%2520Azure%2520SQL%2520using%2520Managed%2520Identity.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FFonsecaSergio%2FScriptCollection%2Fblob%2Fmaster%2FPowershell%2FConnect%2520to%2520Azure%2520SQL%2520using%2520Managed%2520Identity.ps%3C%2FA%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24response%20%3D%20Invoke-WebRequest%20-Uri%20'http%3A%2F%2F169.254.169.254%2Fmetadata%2Fidentity%2Foauth2%2Ftoken%3Fapi-version%3D2018-02-01%26amp%3Bresource%3Dhttps%253A%252F%252Fdatabase.windows.net%252F'%20-Method%20GET%20-Headers%20%40%7BMetadata%3D%22true%22%7D%0A%24content%20%3D%20%24response.Content%20%7C%20ConvertFrom-Json%0A%24AccessToken%20%3D%20%24content.access_token%0A%0A%24SqlConnection%20%3D%20New-Object%20System.Data.SqlClient.SqlConnection%0A%24SqlConnection.ConnectionString%20%3D%20%22Server%3Dtcp%3AServerName.database.windows.net%2C1433%3BInitial%20Catalog%3Dsandbox%3BPersist%20Security%20Info%3DFalse%3BEncrypt%3DTrue%3BTrustServerCertificate%3DFalse%3BConnection%20Timeout%3D30%3B%22%0A%24SqlConnection.AccessToken%20%3D%20%24AccessToken%0A%0Atry%0A%7B%0A%20%20%20%20%24SqlConnection.Open()%0A%0A%20%20%20%20%24SqlCmd%20%3D%20New-Object%20System.Data.SqlClient.SqlCommand%0A%20%20%20%20%24SqlCmd.CommandText%20%3D%20%22SELECT%20SERVERNAME%20%3D%20%40%40SERVERNAME%2C%20SUSER_SNAME%20%3D%20SUSER_SNAME()%3B%22%0A%20%20%20%20%24SqlCmd.Connection%20%3D%20%24SqlConnection%0A%20%20%20%20%24SqlAdapter%20%3D%20New-Object%20System.Data.SqlClient.SqlDataAdapter%0A%20%20%20%20%24SqlAdapter.SelectCommand%20%3D%20%24SqlCmd%0A%20%20%20%20%24DataSet%20%3D%20New-Object%20System.Data.DataSet%0A%20%20%20%20%24SqlAdapter.Fill(%24DataSet)%20%7C%20Out-Null%0A%0A%20%20%20%20Write-Host%20%22Success%20Connection%20%40%40SERVERNAME%20%3D%20%24(%24DataSet.Tables%5B0%5D.Rows%5B0%5D.SERVERNAME)%20%2F%20SUSER_SNAME%20%3D%20%24(%24DataSet.Tables%5B0%5D.Rows%5B0%5D.SUSER_SNAME)%22%20-ForegroundColor%20Green%0A%0A%20%20%20%20%24SqlConnection.Close()%0A%7D%0Acatch%0A%7B%0A%20%20Write-Host%20%22An%20error%20occurred%3A%22%20-ForegroundColor%20Red%0A%20%20Write-Host%20%24_%20-ForegroundColor%20Red%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EIt%20should%20return%20the%20success%20message%20like%20below%0A%3CUL%3E%0A%3CLI%3E%0A%3CPRE%3ESuccess%20Connection%20%40%40SERVERNAME%20%3D%20XXXX%20%2F%20SUSER_SNAME%20%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E96c85223-6377-4f2f-8a6c-8e773fa3b1b2%3C%2FFONT%3E%40%3CFONT%20color%3D%22%230000FF%22%3E72f988bf-86f1-41af-91ab-2d7cd011db%3C%2FFONT%3E%3C%2FPRE%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-90px%22%3E%3CFONT%20size%3D%222%22%3E*%20SUSER_SNAME()%20first%20part%20is%20the%20%3CFONT%20color%3D%22%23FF0000%22%3EApplication%20ID%3C%2FFONT%3E%20we%20found%20on%20Azure%20AD%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-90px%22%3E%3CFONT%20size%3D%222%22%3E*%20SUSER_SNAME()%20second%20part%20is%20your%20%3CFONT%20color%3D%22%230000FF%22%3EAzure%20AD%20Tenant%20ID%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1136266042%22%20id%3D%22toc-hId--1136266042%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-1351246791%22%20id%3D%22toc-hId-1351246791%22%3EScenario%202%3A%20Using%20Azure%20Function%20-%20.NET%3C%2FH3%3E%0A%3CP%3EI've%20create%20an%20Azure%20Function%20sample%20app%20(%3CSTRONG%3EFunction_ConnectSQL_MSI%3C%2FSTRONG%3E)%20that%20you%20can%20get%20it%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FFonsecaSergio%2FAzureFunctionAppTestConnectivity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FFonsecaSergio%2FAzureFunctionAppTestConnectivity%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI've%20used%20a%20NuGet%20called%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.nuget.org%2Fpackages%2FMicrosoft.Azure.Services.AppAuthentication%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMicrosoft.Azure.Services.AppAuthentication%3C%2FA%3E%26nbsp%3Bto%20simplify%20that%20connection%2C%20like%20sample%20below%3A%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%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20azureServiceTokenProvider%20%3D%20new%20AzureServiceTokenProvider()%3B%0Astring%20accessToken%20%3D%20await%20azureServiceTokenProvider.GetAccessTokenAsync(%22https%3A%2F%2Fdatabase.windows.net%2F%22)%3B%0AsqlConnection.AccessToken%20%3D%20accessToken%3B%0A%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%3CP%3EAnd%20was%20able%20to%20connect%20to%20SQL%20without%20the%20need%20to%20provide%20any%20login%20or%20password%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-04-07%2018_45_16-test123fonsecanet%20-%20Function_ConnectSQL_MSI%20-%20Microsoft%20Azure.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%2F182862i64C847EC10866328%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-04-07%2018_45_16-test123fonsecanet%20-%20Function_ConnectSQL_MSI%20-%20Microsoft%20Azure.png%22%20alt%3D%222020-04-07%2018_45_16-test123fonsecanet%20-%20Function_ConnectSQL_MSI%20-%20Microsoft%20Azure.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBe%20aware%20of%20some%20%3CSTRONG%3Elimitations%3C%2FSTRONG%3E%20as%20documented%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Ftroubleshooting-problems-related-to-azure-ad-authentication-with%2Fba-p%2F1062991%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ETroubleshooting%20problems%20related%20to%20Azure%20AD%20authentication%20with%20Azure%20SQL%20DB%20and%20DW%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20width%3D%22696%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2248%22%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E10%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22204%22%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3EService%20principal%20not%20able%20to%20create%20a%20new%20Azure%20AD%20user%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22444%22%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3ECurrently%2C%20this%20operation%20is%20not%20supported.%20To%20help%20us%20evaluate%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3Ethis%20feature%20request%2C%20please%20add%20your%20comments%20to%20the%20blog%20indicated%20below%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Ffeedback.azure.com%252Fforums%252F169401-azure-active-directory%252Fsuggestions%252F39176089-support-service-principal-impersonation-so-that-sp%26amp%3Bdata%3D02%257C01%257Cmireks%2540microsoft.com%257Cab76cf7202064164b04708d778273be0%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C637109982300661462%26amp%3Bsdata%3Dszbq2mxof22cId810CTA47mkR0G5LgW93ElLSYtiPJM%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%20size%3D%222%22%3Ehttps%3A%2F%2Ffeedback.azure.com%2Fforums%2F169401-azure-active-directory%2Fsuggestions%2F39176089-support-service...%3C%2FFONT%3E%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EReferences%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Ftutorial-windows-vm-access-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%20size%3D%222%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Ftutorial-windows-vm-access-sql%3C%2FFONT%3E%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapp-service%2Fapp-service-web-tutorial-connect-msi%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapp-service%2Fapp-service-web-tutorial-connect-msi%3C%2FA%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%3C%2FA%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Fqs-configure-portal-windows-vm%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Fqs-configure-portal-windows-vm%3C%2FA%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%222%22%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Fhow-to-use-vm-token%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Fhow-to-use-vm-token%3C%2FA%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CSPAN%3ETks%20also%20to%26nbsp%3BRenato%20Augusto%20from%20CSS%20(AAD%20team)%20that%20worked%20with%20me%20on%20a%20case%20related%20to%20this%20issue%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1288248%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20article%20we%20will%20explore%26nbsp%3B%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Ftutorial-windows-vm-access-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esystem-assigned%20identity%3C%2FA%3E%3C%2FSTRONG%3E%26nbsp%3Bone%20type%20of%20Managed%20Identity%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1288248%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20SQL%20DB%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Econnectivity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

We all know that we can use SQL authentication or Azure AD authentication to log on Azure SQL DB. We can also use Azure AD Token authentication or certificate-based authentication, but we will not explore these ones here. In this article we will explore Managed Service Identity (MSI) authentication or system-assigned identity, and how to use it on Azure VM (Using Powershell) or on an Azure Function (.NET).

 

Understanding Managed Identity

There are two types of managed identities:

  • system-assigned managed identity is enabled directly on an Azure service instance. When the identity is enabled, Azure creates an identity for the instance in the Azure AD tenant that's trusted by the subscription of the instance. After the identity is created, the credentials are provisioned onto the instance. The lifecycle of a system-assigned identity is directly tied to the Azure service instance that it's enabled on. If the instance is deleted, Azure automatically cleans up the credentials and the identity in Azure AD.
  • user-assigned managed identity is created as a standalone Azure resource. Through a create process, Azure creates an identity in the Azure AD tenant that's trusted by the subscription in use. After the identity is created, the identity can be assigned to one or more Azure service instances. The lifecycle of a user-assigned identity is managed separately from the lifecycle of the Azure service instances to which it's assigned.

Internally, managed identities are service principals of a special type, which are locked to only be used with Azure resources. When the managed identity is deleted, the corresponding service principal is automatically removed.

 

Your code can use a managed identity to request access tokens for services that support Azure AD authentication. Azure takes care of rolling the credentials that are used by the service instance.

The following diagram shows how managed service identities work with Azure virtual machines (VMs):

data-flow.png

How a system-assigned managed identity works with an Azure VM

  1. Azure Resource Manager receives a request to enable the system-assigned managed identity on a VM.

  2. Azure Resource Manager creates a service principal in Azure AD for the identity of the VM. The service principal is created in the Azure AD tenant that's trusted by the subscription.

  3. Azure Resource Manager configures the identity on the VM by updating the Azure Instance Metadata Service identity endpoint with the service principal client ID and certificate.

  4. After the VM has an identity, use the service principal information to grant the VM access to Azure resources. To call Azure Resource Manager, use role-based access control (RBAC) in Azure AD to assign the appropriate role to the VM service principal. To call Key Vault, grant your code access to the specific secret or key in Key Vault.

  5. Your code that's running on the VM can request a token from the Azure Instance Metadata service endpoint, accessible only from within the VM: http://169.254.169.254/metadata/identity/oauth2/token

    • The resource parameter specifies the service to which the token is sent. To authenticate to Azure Resource Manager, use resource=https://management.azure.com/.
    • API version parameter specifies the IMDS version, use api-version=2018-02-01 or greater.
  6. A call is made to Azure AD to request an access token (as specified in step 5) by using the client ID and certificate configured in step 3. Azure AD returns a JSON Web Token (JWT) access token.

  7. Your code sends the access token on a call to a service that supports Azure AD authentication.

 

Hands On

To assign an Identity to an Azure VM you can just go to Azure Portal and enable it

FonsecaSergio_0-1586264008652.png

 

You will be able to find this identity on Azure Active Directory > Enterprise applications

2020-04-07 15_10_12-Clipboard.png

 

2020-04-07 15_12_00-Enterprise applications _ All applications - Microsoft Azure.png

 

 

You can also get and Identity on an Azure Webapp or Azure Function like sample below

2020-04-07 14_00_51-Clipboard.png

 

2020-04-07 14_04_09-Edit Article - Microsoft Tech Community.png

 

Giving SQL Permission

  • First make sure your Azure SQL DB have an AAD Admin provisioned
  • Go to database and add it like adding a regular AAD user, using name of the identity (VM or name of function)
DROP USER IF EXISTS [VMSQLCLient]
GO
CREATE USER [VMSQLCLient] FROM EXTERNAL PROVIDER;
GO
ALTER ROLE db_datareader ADD MEMBER [VMSQLCLient];
ALTER ROLE db_datawriter ADD MEMBER [VMSQLCLient];
GRANT EXECUTE TO [VMSQLCLient]
GO
---------------------------------------------------
DROP USER IF EXISTS [test123fonsecanet]
GO
CREATE USER [test123fonsecanet] FROM EXTERNAL PROVIDER;
GO
ALTER ROLE db_datareader ADD MEMBER [test123fonsecanet];
ALTER ROLE db_datawriter ADD MEMBER [test123fonsecanet];
GRANT EXECUTE TO [test123fonsecanet]
GO

 

Scenario 1: Using Azure VM - Powershell test

  • After assigning MSI to VM, and granting the required permissions on SQL. Start the VM
  • Open Powershell ISE
  • Run a test script like sample below
    • https://github.com/FonsecaSergio/ScriptCollection/blob/master/Powershell/Connect%20to%20Azure%20SQL%...
    • $response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.windows.net%2F' -Method GET -Headers @{Metadata="true"}
      $content = $response.Content | ConvertFrom-Json
      $AccessToken = $content.access_token
      
      $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
      $SqlConnection.ConnectionString = "Server=tcp:ServerName.database.windows.net,1433;Initial Catalog=sandbox;Persist Security Info=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
      $SqlConnection.AccessToken = $AccessToken
      
      try
      {
          $SqlConnection.Open()
      
          $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
          $SqlCmd.CommandText = "SELECT SERVERNAME = @@SERVERNAME, SUSER_SNAME = SUSER_SNAME();"
          $SqlCmd.Connection = $SqlConnection
          $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
          $SqlAdapter.SelectCommand = $SqlCmd
          $DataSet = New-Object System.Data.DataSet
          $SqlAdapter.Fill($DataSet) | Out-Null
      
          Write-Host "Success Connection @@SERVERNAME = $($DataSet.Tables[0].Rows[0].SERVERNAME) / SUSER_SNAME = $($DataSet.Tables[0].Rows[0].SUSER_SNAME)" -ForegroundColor Green
      
          $SqlConnection.Close()
      }
      catch
      {
        Write-Host "An error occurred:" -ForegroundColor Red
        Write-Host $_ -ForegroundColor Red
      }
      
  • It should return the success message like below
    • Success Connection @@SERVERNAME = XXXX / SUSER_SNAME = 96c85223-6377-4f2f-8a6c-8e773fa3b1b2@72f988bf-86f1-41af-91ab-2d7cd011db

* SUSER_SNAME() first part is the Application ID we found on Azure AD

* SUSER_SNAME() second part is your Azure AD Tenant ID

 

Scenario 2: Using Azure Function - .NET

I've create an Azure Function sample app (Function_ConnectSQL_MSI) that you can get it from https://github.com/FonsecaSergio/AzureFunctionAppTestConnectivity

 

I've used a NuGet called Microsoft.Azure.Services.AppAuthentication to simplify that connection, like sample below:

 

 

 

 

var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://database.windows.net/");
sqlConnection.AccessToken = accessToken;

 

 

 

 

And was able to connect to SQL without the need to provide any login or password

2020-04-07 18_45_16-test123fonsecanet - Function_ConnectSQL_MSI - Microsoft Azure.png

 

Be aware of some limitations as documented at Troubleshooting problems related to Azure AD authentication with Azure SQL DB and DW.

 

 

10

Service principal not able to create a new Azure AD user

Currently, this operation is not supported. To help us evaluate

this feature request, please add your comments to the blog indicated below:

https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/39176089-support-service...

 

References:

Tks also to Renato Augusto from CSS (AAD team) that worked with me on a case related to this issue