No, you can't. There is a preview feature as mentioned in the comment, it uses azure ad RBAC role to authorize access to eventhub, but this is just another different way, if someone knows your connection string, he can also send the events.
My workaround is to use the Azure Keyvault to store the connection string as a secret. Then you can set the Access policies
of the keyvault, add the user/service principal which you want. Then only the users/service principals with the permissions can retrieve the secret.
In the sample which you mentioned, it uses the plain text
static string connectionString = "namespace connection string"
, I suppose you may think it is not safe. After you store the connection string in the keyvault, you could use keyVaultClient.GetSecretAsync
to get the secret to avoid to expose the connection string.
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("https://<YourKeyVaultName>.vault.azure.net/secrets/AppSecret")
.ConfigureAwait(false);
Message = secret.Value;
For more details, you could refer to the links below.