c# - Securing controller action called from a webjob -
i have mvc web site deployed in azure, need allow users generate pdf file html page. invoking wkhtmltopdf.exe within webjob called controller action. html page rendered produced controller action returns actionresult.
everything works fine when decorate action (that renders html pdf) [allowanonymous] secure somehow.
is possible authenticate request web job or have mysecureaction return data requests originate web job?
here's web job code:
static void main(string[] args) { if (args.length == 2) { var url = args[0]; var filename = args[1]; try { using (var p = new system.diagnostics.process()) { var startinfo = new system.diagnostics.processstartinfo { filename = "wkhtmltopdf.exe", arguments = url + " " + filename, useshellexecute = false, }; p.startinfo = startinfo; p.start(); p.waitforexit(); p.close(); } // here save pdf file azure blob storage } catch (exception ex) { /*error handling*/ } } }
and here's code calls web job:
string baseurl = request.url.scheme + "://" + request.url.authority + request.applicationpath.trimend('/'); string url = baseurl + "/mycontroller/mysecureaction/" + id.tostring(); string filename = "filename.pdf"; try { using (var client = new httpclient()) { client.baseaddress = new uri("https://myazurewebapp.scm.azurewebsites.net/"); client.defaultrequestheaders.accept.clear(); var username = "$myazurewebappuser"; var password = "myazurewebapppassword"; var encoding = new asciiencoding(); var authheader = new authenticationheadervalue("basic", convert.tobase64string( encoding.getbytes(string.format($"{username}:{password}")))); client.defaultrequestheaders.authorization = authheader; var content = new system.net.http.stringcontent(""); httpresponsemessage response = await client.postasync($"api/triggeredwebjobs/mywebjob/run?arguments={url} {filename}", content); if (!response.issuccessstatuscode) { //error handling } } } catch (exception ex) { //error handling } byte[] file = null; try { using (var client = new webclient()) { // retrieve file blob storage file = client.downloaddata("https://myazureaccount.blob.core.windows.net/pdf/" + filename); } } catch (exception ex) { /*error handling*/ } // return file user
and here's action web job calls html
[allowanonymous] public actionresult mysecureaction(int? id) { somedata model = new somedata(); // data db using id return view(model); }
this not seem use of webjob. webjobs not invoked web application, nor send requests web app. instead, consider couple alternatives:
you can work directly within app instead of using webjob, isn't buying here.
you can have communication between app , webjob done via queue instead of via direct http messages. e.g. web app adds work item queue, , webjob picks them up, e.g. using webjobs sdk.
Comments
Post a Comment