Answered by:
BackgroundUploader credentials

Question
-
Hi,
I'm trying to upload a file to a secure virtual directory on a server. I'm using the BackgroundUploader class configured as follows:
Uri uri = new Uri("http://server-name/Images"); StorageFile file = await GetStorageFile("TEST.txt"); BackgroundUploader uploader = new BackgroundUploader(); uploader.Method = "PUT"; // I tried "POST" too PasswordCredential cred = new PasswordCredential(); cred.UserName = "domain\username"; cred.Password = "password"; uploader.ServerCredential = cred;
// I also tried using ProxyCredential
uploader.SetRequestHeader("Filename", file.Name); UploadOperation upload = uploader.CreateUpload(uri, file); Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress); CancellationTokenSource cts = new CancellationTokenSource(); await upload.StartAsync().AsTask(cts.Token, progressCallback); // throws an exception ... private void UploadProgress(UploadOperation operation) { }
public static async Task<StorageFile> GetStorageFile(string filename)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
try
{
StorageFile file = await localFolder.GetFileAsync(filename);
return file;
}
catch (FileNotFoundException exc)
{
Debug.WriteLine("Exception: " + exc.Message);
return null;
}
}Every time I run the code, the upload.StartAsync().AsTask(cts.Token, progressCallback) line throws an exception:
Unauthorized (401). (Exception from HRESULT: 0x80190191)
The credentials are correct. Has anyone made it work?
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.com- Edited by ata6502 Wednesday, October 23, 2013 3:03 PM code
Wednesday, October 23, 2013 3:01 PM
Answers
-
Hello,
I cannot reproduce the issue. Here's what I did.
- I used the BackgroundTransfer sample and modified ScenarioInput2.xaml.cs file and specifically the function StartUpload_Click to add the credentials.
- I ran the SetupServer.ps1 file from an elevated command prompt to setup the upload page on my local IIS web server and then modified the application in IIS manager to request Authentication. I tested with both the types: Basic and Windows Authentication and each time the request ran fine without any errors.
- If you are getting the error constantly, I would recommend that you debug your issue using Fiddler (www.fiddlertool.com) and find out whether the HTTP 401 error you are receiving is because your user got authenticated, but did not have the necessary permissions to perform a "write" to the location where you are trying to upload/ PUT to.
- If your target is IIS, you can collect a Failed Request trace on IIS to understand why the 401 error was thrown.
Here's the code I used (notice the \\ after the domain name:
BackgroundUploader uploader = new BackgroundUploader(); uploader.SetRequestHeader("Filename", file.Name); // Set the credential PasswordCredential pwdCredential = new PasswordCredential(); pwdCredential.UserName = "domain\\user"; pwdCredential.Password = "Password"; uploader.ServerCredential = pwdCredential; UploadOperation upload = uploader.CreateUpload(uri, file); Log(String.Format("Uploading {0} to {1}, {2}", file.Name, uri.AbsoluteUri, upload.Guid));
Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog
- Marked as answer by ata6502 Wednesday, October 23, 2013 10:57 PM
Wednesday, October 23, 2013 9:02 PMModerator
All replies
-
Hello,
I cannot reproduce the issue. Here's what I did.
- I used the BackgroundTransfer sample and modified ScenarioInput2.xaml.cs file and specifically the function StartUpload_Click to add the credentials.
- I ran the SetupServer.ps1 file from an elevated command prompt to setup the upload page on my local IIS web server and then modified the application in IIS manager to request Authentication. I tested with both the types: Basic and Windows Authentication and each time the request ran fine without any errors.
- If you are getting the error constantly, I would recommend that you debug your issue using Fiddler (www.fiddlertool.com) and find out whether the HTTP 401 error you are receiving is because your user got authenticated, but did not have the necessary permissions to perform a "write" to the location where you are trying to upload/ PUT to.
- If your target is IIS, you can collect a Failed Request trace on IIS to understand why the 401 error was thrown.
Here's the code I used (notice the \\ after the domain name:
BackgroundUploader uploader = new BackgroundUploader(); uploader.SetRequestHeader("Filename", file.Name); // Set the credential PasswordCredential pwdCredential = new PasswordCredential(); pwdCredential.UserName = "domain\\user"; pwdCredential.Password = "Password"; uploader.ServerCredential = pwdCredential; UploadOperation upload = uploader.CreateUpload(uri, file); Log(String.Format("Uploading {0} to {1}, {2}", file.Name, uri.AbsoluteUri, upload.Guid));
Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog
- Marked as answer by ata6502 Wednesday, October 23, 2013 10:57 PM
Wednesday, October 23, 2013 9:02 PMModerator -
Thanks Prashant for the response. It's good to know the code should work fine with the ServerCredentials. The '\' vs. '\\' could contribute to the problem too.
I will have a look at the Fiddler tool. It's quite possible that the website I'm connecting to is configured improperly.
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.comWednesday, October 23, 2013 10:57 PM