首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保护从网络作业中调用的控制器操作

保护从网络作业中调用的控制器操作
EN

Stack Overflow用户
提问于 2017-09-12 09:21:35
回答 1查看 612关注 0票数 0

我有一个MVC网站部署在Azure,我需要允许用户从一个html页面生成一个pdf文件。为此,我在从控制器操作调用的wkhtmltopdf.exe中调用WebJob。呈现的html页面是由返回ActionResult的另一个控制器操作生成的。

当我用AllowAnonymous装饰这个动作(为pdf呈现html )时,一切都很好,但我想以某种方式保护它。

是否有可能对来自web作业的请求进行身份验证,或者让mysecureaction只将其数据返回给来自web作业的请求?

这是我的网络工作代码:

代码语言:javascript
复制
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 the pdf file to azure blob storage
    } 
    catch (Exception ex) { /*error handling*/ }
    }
  }

下面是调用网络作业的代码:

代码语言:javascript
复制
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 the file from blob storage
    file = client.DownloadData("https://myazureaccount.blob.core.windows.net/pdf/" + filename);
  }
}
catch (Exception ex) { /*error handling*/ }

// return the file to the user

很明显,这是web作业调用的操作,以获得html

代码语言:javascript
复制
[AllowAnonymous]
public ActionResult mysecureaction(int? id)
{
  SomeData model = new SomeData();
  // get some data from db using id
  return View(model);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-12 21:35:44

这似乎不太好地使用WebJob。WebJobs通常不会被web应用程序调用,它们本身也不会向web应用程序发送请求。相反,考虑几个备选方案:

你可以直接在你的应用程序中完成这项工作,而不是使用WebJob,因为它在这里给你买的东西不多。

您可以通过队列而不是通过直接的http消息在应用程序和WebJob之间进行通信。例如,web应用程序将工作项添加到队列中,然后WebJob获取它们,例如使用WebJobs SDK。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46172617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档