Quote:
string[] str = response.RequestMessage.RequestUri.LocalPath.Split('/');
...
str[str.Length - 1] = str[str.Length - 1].Replace(".aspx", ".pdf");
var fileInfo = new FileInfo(str[str.Length - 1]);
You are trying to write a file in the current working directory.
On Azure, that working directory is the root of your site:
D:\home\site\wwwroot
The user your application is running as on Azure does not have permission to write to the root of the application. And it should not have that permission, since this would represent a potential security vulnerability.
Instead, you should be writing the file to the temporary folder:
string tempFolder = Path.GetTempPath();
string filePath = Path.Combine(tempFolder, str[str.Length - 1]);
var fileInfo = new FileInfo(filePath);
But remember, your code could be accessed by multiple users at the same time. If you write the attachment to a file with a fixed name like
PDF_Report
, the file for one user could overwrite the file for another user. You could end up sending corrupted files as attachments. Or you could end up exposing private data belonging to one user to other users, which would mean you would face large fines.