-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authenticate with Azure AD #229
Comments
Hi @rdagumampan, |
@pmelander, thanks again for reaching out. When I look at the docs, it doesnt seem to be straight forward. If user has to use Service Principal, there seems to be some change to be made in the way connection is established. Feel free to pick this up if this needs code changes.
|
I think you run into this problem as you not using var sqlConnectionString = "Data Source=tcp:[servername].database.windows.net,1433;Initial Catalog=[databasename];Connect Timeout=30;Encrypt=True;Authentication=Active Directory Default";
using (var conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (var cmd = new SqlCommand("SELECT SUSER_SNAME()", conn))
{
var result = cmd.ExecuteScalar();
result.Dump();
}
} Although token authentication can be used when authenticating with AAD, using Active Directory Managed Identity simplifies authentication, extending login solutions to the client environment, Visual Studio Code, Visual Studio, Azure CLI etc.
|
@rdagumampan Here is a step by step instruction on how to setup AAD MSI for an Azure web service using ARM template deployment. Enabling Managed Service IdentityAdd the identity section to the App service or Function app deployment (ARM) template and set type to SystemAssigned. resource site_resource 'Microsoft.Web/sites@2020-06-01' = {
name: appName
location: location
kind: 'appservice'
tags: resourceTags
identity: {
type: 'SystemAssigned'
} Once the web application has been created in Azure, query the identity information from the resource using Azure CLI:
We should get an output like this: {
"principalId": "f76495ad-d682-xxxx-xxxx-bc70710ebf0e",
"tenantId": "8305b292-c023-xxxx-xxxx-a042eb5bceb5",
"type": null
} Creating SQL UsersAzure SQL Database does not support creating logins or users from service principals created from Managed Service Identity. The only way to provide access to one is to add it to an AAD group, and then grant access to the group to the database. We can use the Azure CLI to add our MSI to the appropriate group, we're passing the principalId from the previous query.
With the service principal name added to the group, we can add the group as a database user and add it to the db_datareader and db_datawriter roles and grant execute to schemas in MSSMS (sql management studio) DECLARE @PrincipalName NVARCHAR(max), @SchemaName NVARCHAR(max), @sql NVARCHAR(max)
SELECT @PrincipalName = '<Group Name>', @SchemaName = '<SchemaName>'
IF NOT EXISTS (
SELECT 0 FROM [sys].[database_principals]
WHERE [name] = @PrincipalName)
BEGIN
SELECT @sql = 'CREATE USER ['+ @PrincipalName + '] FROM EXTERNAL PROVIDER'
EXEC(@sql);
SELECT @sql = 'ALTER ROLE db_datareader ADD MEMBER [' + @PrincipalName + ']'
EXEC(@sql);
SELECT @sql = 'ALTER ROLE db_datawriter ADD MEMBER [' + @PrincipalName + ']'
EXEC(@sql);
END
SELECT @sql = 'GRANT EXECUTE ON SCHEMA::' + @SchemaName + ' TO [' + @PrincipalName + ']'
EXEC(@sql) |
@pmelander I think there are two use cases here and you described one where it works like "Integrated Security" mode. From what I read, a |
Closed. Released in |
https://dba.stackexchange.com/questions/184598/unable-to-connect-using-azure-ad-service-principal-on-sql-server
https://michaelcollier.wordpress.com/2016/11/03/connect-to-azure-sql-database-by-using-azure-ad-authentication/
https://techcommunity.microsoft.com/t5/azure-sql-blog/azure-ad-service-principal-authentication-to-sql-db-code-sample/ba-p/481467
Managed Identity vs Service Principal
https://stackoverflow.com/questions/61322079/difference-between-service-principal-and-managed-identities-in-azure
https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals#service-principal-object
Token lifetime
https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-configurable-token-lifetimes
https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens#access-token-lifetime
The text was updated successfully, but these errors were encountered: