Reputation: 33
I have an azure table with columns
[PartitionKey],[RowKey],[UserId],[UserName],[Email].
I want to encrypt only the Username
and Emails
. Is there any way to do this in Azure Table?
Any help is appreciated.
Thanks in advance.
Upvotes: 1
Views: 693
Reputation: 24529
Is there any way to do this in Azure Table?
Yes, in your entity mark properties with attribute EncryptProperty
[EncryptProperty]
public string UserName { get; set; }
[EncryptProperty]
public string Email { get; set; }
We also could refer to this document to get more information about how to encrypt table entity.
I also do a demo for it, following is the detail steps.
1.Create a .net console application
2.Install the WindowsAzure.Storage and Microsoft.Azure.KeyVault.Extensions with nuget
3.Add a new class named User with following code.
public class User:TableEntity
{
public string UserId { get; set; }
[EncryptProperty]
public string UserName { get; set; }
[EncryptProperty]
public string Email { get; set; }
public User()
{
PartitionKey = "Tom";
RowKey = Guid.NewGuid().ToString();
}
public User(string userId, string userName, string email)
{
PartitionKey = "Tom";
RowKey = Guid.NewGuid().ToString();
UserId = userId;
UserName = userName;
Email = email;
}
}
4.Add the test code in the Program.cs
static void Main(string[] args)
{
var connectionstring = "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=accountKey";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
RsaKey key = new RsaKey("mykey" /* key identifier */);
// Create the encryption policy to be used for upload and download.
TableEncryptionPolicy policy = new TableEncryptionPolicy(key, null);
TableRequestOptions options = new TableRequestOptions
{
EncryptionPolicy = policy
};
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the CloudTable object that represents the "tomtest" table.
CloudTable table = tableClient.GetTableReference("tomtest");
table.CreateIfNotExists();
//var insertList = new List<User>();
var user = new User { UserId = Guid.NewGuid().ToString(),UserName="tom1",Email="[email protected]" };
table.Execute(TableOperation.Insert(user), options);
TableRequestOptions retrieveoptions = new TableRequestOptions
{
EncryptionPolicy = policy
};
var query = new TableQuery<User>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, user.RowKey));
var list = table.ExecuteQuery(query, retrieveoptions);
foreach (User entity in list)
{
Console.WriteLine($"PartionKey:{entity.PartitionKey},RowKey:{entity.RowKey},userId:{entity.UserId},UserName: {entity.UserName},email:{entity.Email}");
}
Console.ReadKey();
}
5.Check it with Microsoft Azure storage exploer
6.Retrieve the table entity from table and output to console
Upvotes: 4