Reputation: 10390
I am retrieving user profile pictures from SharePoint then displaying those users on a page, settings the src
attribute in img
element to the url of the image.
It works perfectly when debugging in VS.
However, after I publish to Azure, the images are not loading. What is strange is that after I click on the link for the image, it seems to authenticate me, and then the images DO start loading...
How can I just pass in the current users credentials automatically so my users don't have to navigate to the image itself before seeing them in the web app?
example of an element:
<img src="https://companyname-my.sharepoint.com:443/User%20Photos/Profile%20Pictures/firstname_lastname_company_com_MThumb.jpg?t=63655312814" class="userPic">
The currently signed in user is obviously trusted, as it does work after going to that url - can't I add a paramater of some kind to that address to do it automatically?
Really appreciate any help with this.
Upvotes: 0
Views: 649
Reputation: 473
With Azure storage, if an image has been saved as private then it will require a sharedaccesssignature.
For any images i have for my own sites i have a class like the below:
public string GetAzureStoreImage(string fileName, string containerName)
{
string imageUrl = string.Empty;
// Connect to cloud storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("YourStoredConnectionString"));
// Connect blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Connect to container
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Get reference for current file
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
if (blob.Exists())
{
// Create access token for file share
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=" + fileName
});
// Collect url and download file
imageUrl = string.Format("{0}{1}", blob.Uri, sasToken);
}
return imageUrl;
}
You will need to include the Microsoft.WindowsAzure.Storage and Microsoft.WindowsAzure.Storage.Blob references for them, but this will give a temporary 10 minute token to access the file that will then allow the returned "imageurl" to be your images source.
Finally the fileName is just the filename you would like to collect from your Azure storage, and the containerName is what "bucket" houses the current file.
You may also need to pass in another parameter based on the current users information if they all have their own security tokens to access images which should be appended to your connection string.
Upvotes: 1