Externalize config (password, connectionString) in UWP - config

I'm looking for a way to externalize some config in an UWP application.
The application need to connect to an Alfresco Server. And I want the user to be able to configure the Login, Password, and the url of the server.
I tried to put an xml file in my assets file, but when the App is installed, it's locate in C:\Program Files\WindowsApps, and the user can't have an easy access to it.
Is there any way to acheiev this like with an app.config file ?

A possibility is to use a known StorageFolder like :
KnownFolders.DocumentsLibrary
If you add a file type association into you manifest (Package.appxmanifest > Declarations > File Type Associations) for a particular extension (like .config), your app can read files of this type in the document folder. Users can edit theses files manualy too.
Note all files you include in your assets (in your package) are read-only files.
Another way if your users are experienced : they can edit a file in the local storage (with no need to make a file association into your manifest). In this case, you use the following StorageFolder :
ApplicationData.Current.LocalFolder
And users can edit files into a specifique directory :
C:\Users\<UserName>\AppData\Local\Packages\<AppId>\AppData

I had a very similar issue where I needed to use an external file that is not tracked by source control. There are two ways to access data from resource or configuration files.
One is to open and parse a configuration file. Given a file sample.txt with Build Action Content (Copy to Output Directory doesn't matter), we can read it with
var uri = new System.Uri("ms-appx:///sample.txt");
var sampleFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
or
var packageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var sampleFile = await packageFolder.GetFileAsync("sample.txt");
followed by
var contents = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
Alternatively, we can use Resources. Add a new Resource item to the project, called resourcesFile.resw. To access data, use:
var resources = new Windows.ApplicationModel.Resources.ResourceLoader("resourcesFile");
var token = resources.GetString("secret");
I wrote more verbose answer in a blog post Custom resource files in UWP

Related

Uploading and saving a file to IIS virtual directory outside the root folder

Based on Microsoft's website as well as even this forum the general consensus seems to be to keep uploaded user files from the website outside of the wwwroot folder (C:\inetpub\wwwroot) and even better outside of the system drive. I have setup a virtual directory(C:\inetpub\files) in IIS for my file uploads which is outside of the wwwroot but still on the C drive (we only have one drive and I cannot partition it to make another drive). So hopefully this is still considered secure in that aspect! My issue however is I use the following code to get the directory to my hosting enviroment:
var filePath = Path.Combine(env.WebRootPath, document.DOCUMENT_LOCATION);
var fileName = document.FILENAME_AFTER_UPLOAD;
var fullPath = Path.Combine(filePath, fileName);
I am not sure exactly what file path I am suppose to use for saving to virtual directory. The virtual directory has an alias of "files" so its virtual path is /files so do I use env.WebRootPath + "/files" or is there some other way to access the virtual directory/path? For background document is a model object from a SQL query that returns my file path to save to and the filename we create in the SQL server.
So,you want to upload a file outside of that in the parent directory of env.webrootpath means wwwroot folder.so for that try this below code:-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", document.Document.FileName);
above, env.webrootpath no need to use because you want to path more dynamic.
Or if you want to upload to C drive instead of wwwroot.
string SavePath = Path.Combine(Directory.GetCurrentDirectory(), (#"C:\", model.FormFile.FileName);

How to get file path in Asp.net core to work in Azure Dev Ops

Am trying to read Text File content. So i am using below line of code it is working in local .
string src = #"Templates\UserMailTemplate.txt";
string[] content = System.IO.File.ReadAllLines(src);
After hosting in Azure Dev Ops .Am getting issue as
Could not find file 'D:\home\site\wwwroot\Templates\UserMailTemplate.txt'.
Server.Mappath is not working in ASP.Net Core .
Please let me know how can i resolve this in DEV ops.
The cause of your issue is in the error message: Could not find file. This probably means the file is either not published, or not published at the location you're expecting it to be.
Please validate that you're looking in the right location.
To enable having the files available after publish, either
1. Have a look at IHostingEnvironment and its properties
2. Set the Build Action to Content and access it using Application.GetContentStream
3. Set the Build Action to Embedded Resource and call Assembly.GetManifestResourceStream to read the file from the assembly
4. Set Copy to Output Directory to Copy always or Copy if newer and access the files in the output directory
'Slightly' off-topic
Please have a look at a completely different way to manage mail templates in Azure. The easiest change from what you're doing now is probably to store them in Azure Storage.
If you persist in using files, please refer to this Azure Tip: Working with Files in Azure App Service

Asp.net core file download corrupt

I've been trying to provide an epub download in various ways. All of them work when downloading on my laptop with any browser, but when downloading with the e-reader it results in either a "file is corrupt" or "content type not supported". The problem is not with the file itself: When I upload it to any other place (e.g. public file dump websites) I can download the file without any issues to my e-reader.
Here's one of the many ways I've tried:
IFileProvider provider = new PhysicalFileProvider(path);
IFileInfo fileInfo = provider.GetFileInfo(filename);
var readStream = fileInfo.CreateReadStream();
var fileType = "application/epub+zip"; //MediaTypeNames.Application.Octet
return File(readStream, fileType, Path.GetFileName(outputFilepath));
and on the razor page e.g.:
Epub2
Epub2
(here the first link results in "corrupt file" and the second in "content type not supported).
On the server, the file is placed outside the website root.
What are some possible reasons that the direct download to my e-reader doesn't work with this code, yet with plain file uploads/downloads it works?
Thanks a lot for your help!
The issue was the download over https to my Tolino e-reader. This specific old Tolino model has issues with downloads over https, when I switched to http I could successfully download the ebook.

Is there a root folder I can access in Azure File Storage?

I am accessing Azure File Storage from Silverlight and I need to upload/create a crossdomain.xml file at the root of the storage:
https://[account].file.core.windows.net/crossdomain.xml
Seen many example for blob on this but I can't seem to create a $root for the file storage (without adding a file share).
Silverlight requires a crossdomain.xml at the root level to use the Azure REST API.
Any ideas?
Azure File Storage follows the "share-directory" structure. I am not aware about any Root folder (i know about crossdomain and the requirements) in Azure File Storage and i believe it can not be done with the File Storage.
You can invoke
CloudFileDirectory rootDir = share.getRootDirectoryReference()
which returns the share root directory, but not File Storage one.
For being able to implement what you need i would recommend you to switch to Blob Storage if there are no specific requirements that need File Storage.
The comment section did not allow to that much characters. Therefore, I've posted this as an answer which in fact is not a solution :).
The following is my experience which I've wanted to share.
I have tried to add a crossdomain file into the root but could not succeed. The code is as follows:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(getConnectionString());
CloudFileClient client = storageAccount.CreateCloudFileClient();
CloudFileShare share = client.GetShareReference(shareName);
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
var crossDomainFile = share.GetRootDirectoryReference().GetFileReference("crossdomain.xml");
if (!crossDomainFile.Exists())
{
crossDomainFile.UploadText(getCrossDomainXmlContent());
Console.WriteLine("crossDomainFile.StorageUri:" + crossDomainFile.StorageUri.PrimaryUri);
}
share.GetRootDirectoryReference().GetFileReference("crossdomain.xml") which gives you this address:
https://[account].file.core.windows.net/<share name>/crossdomain.xml.
For Silverlight or flash clients, the file must reside at https://[account].file.core.windows.net/crossdomain.xml.
Using blob storage instead of file is a better choice, it seems.
Also creating a cloud file object by giving a URI like https://[account].file.core.windows.net/crossdomain.xml is also useless. When you call UploadText method (example: crossDomainFile.UploadText(getCrossDomainXmlContent()), you get 400 - Bad Request error.

how to make a link to a S3 file download rather then display

If I have a file stored on amazon S3 and I want it to download when the user clicks on it normally, how can I accomplish that?
I found some information on setting the content disposition(?) but I can't find anything actually providing instructions on how to do so.
You can select file > properties > meta-data and add the content-disposition header as an option there.
Check Content-Disponsition in your File S3 or when upload the file.
You can configure it by using setContentDisposition in sdk
In java
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentDisposition("attachment; filename=...");
By this when a user clicks on the link file will be downloaded