Piotr Stapp
Piotr Stapp

Reputation: 19830

Upload file form powershell to asp.net

I have an asp.net mvc application, which allows to upload company structure using CSV file. I was asked about possibility to automate this function using powershell script.Creating CSV in powershell is easy, but I do not have an idea how upload to asp.net.

My first choice was to use WebClient, but I have problem with authentication - in mvc we are using forms authentication.I read here that it is possible, but if my login form changes I will have to send updated script to client. I would like to omit mange code on client side.

The second option is to crate separate controller and use in it authorization token, but I look like "inventing a wheel again", because I would need to write all code responsible for authentication.

Can I improve one of above options? Or maybe there is a better choice?

Upvotes: 7

Views: 1334

Answers (3)

Burt_Harris
Burt_Harris

Reputation: 6874

You might be able to use the existing web service using the Invoke-RestMethod cmdlet, but it could take two separate invokes. For both invocations you'll probably need to say -Method Post. Its possible to do all this with WebClient, but the cmdlet may be easier to describe. I haven't actually tried this, but it could look something like this:

Invoke-RestMethod -Method Post $loginPage -SessionVariable webSession -Body "..."
Invoke-RestMethod -Method Post $uploadPage -WebSession $webSession -Body "..."

For the first invocation, you specify the URL of the login page, and would simulate a web forms login by providing a username and password in the -Body parmeter, and use -SessionVariable to capture and store context for making the next request(s) authenticated.

In the second request, you use your data upload URL, the -WebSession parameter to supply the context established by the first request, and -Body to upload your CSV. Note that you need the dollar sign on the webSession variable in the second one, but not the first.

Of course, you'll need to store the username/password for the automation to use somewhere, but that's always needed for unattended automation. A possible slick approach to mitigate this would be to use client certificate-based credentials rather than a web form authentication.

Upvotes: 1

RuntimeError
RuntimeError

Reputation: 105

I would probably do it like this:

  • Step 1: Create an https webpage on the asp.net server to receive the csv
  • Step 2: Create a powershell script that calls curl with the -F option to post a file to it and append any metadata you need on the call
  • Step 3: Upon receiving the file, store it using the metadata provided in the form and append clientid/date/etc to the file

Upvotes: 0

vonPryz
vonPryz

Reputation: 24071

How about using a side channel?

There are two approaches. You either send it to the customer's web server every now and then, or the web server downloads the data from you.

For sending data, FTP over SSL should be secure enough. Here is an example about how to command FTP with Powershell. FTP/SSL is easy enough to configure for IIS.

For receiving data, just publish the CSV on your own web site. Set up a script on the customer's web server that downloads CSV every now and then. If the CSV should not be accessible to anyone but the customer, require a client certificate.

Upvotes: 0

Related Questions