【问题标题】:Authentication failing with multiple threads c#多线程身份验证失败c#
【发布时间】:2015-03-24 15:57:35
【问题描述】:

我目前正在编写一个提供一些数据库操作的 Web 应用程序。系统还需要能够处理不同的数据库。问题是数据库中的某些字段必须使用来自 RESTful api 的数据进行更新。为了执行这些更新,我使用 Quartz.NET 调度为每个需要更新的表创建一个作业。这个作业检查它是否需要更新相关的数据库。如果是,它会从 REST 服务获取信息。当我不使用多线程时,访问 REST 服务没有问题。但是当我使用 Quartz.net 时,服务器会为某些请求返回 401 错误。这种行为是不一致的,并不总是发生。我正在使用 RestSharper 使用自定义身份验证器进行这些调用以进行摘要身份验证。我在这里得到了这个解决方案:
http://www.ifjeffcandoit.com/2013/05/16/digest-authentication-with-restsharp/

我还在该帖子中使用了身份验证修复程序。

创建工作:

            foreach (KeyValuePair<string, int> tabel in DataParser.getAllTabellenMetRefresh())
        {
            // define the job and tie it to our class
            IJobDetail job = JobBuilder.Create<LocatieUpdater>()
                .WithIdentity("dbUpdaterJob"+count, "group1")
                .UsingJobData("type", tabel.Key)
                .Build();

            // Trigger the job to run now, and then every x seconds
            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("myTrigger"+count, "group1")
              .StartNow()
              .WithSimpleSchedule(x => x
                  .WithIntervalInMinutes(tabel.Value)
                  .RepeatForever())
              .Build();
            sched.ScheduleJob(job, trigger);

这段代码中的响应收到 401。

var client = new RestClient(gegevens["Url"]);

        client.Authenticator = new DigestAuthenticator(ConfigAcces.getCredentials()[0], ConfigAcces.getCredentials()[1]);


        var request = new RestRequest(gegevens["request"], Method.GET);
        request.AddParameter("fields", "all");
        request.AddParameter(velden["TagId"], id);
        IRestResponse response = client.Execute(request);

我用 fiddler 查看了请求,但没有真正看到任何有用的东西。非常欢迎任何帮助或提示。 提前致谢!

【问题讨论】:

    标签: c# .net quartz.net restsharp


    【解决方案1】:

    不完全确定这是如何解决的,但所有身份验证似乎都可以通过禁用 SyncronisationContext 并使该方法等待任务来工作。

      var request = new RestRequest(gegevens["request"], Method.GET);
        request.AddParameter("fields", "all");
        request.AddParameter(velden["TagId"], id);
            client.UseSynchronizationContext = false;
            IRestResponse response = await client.ExecuteTaskAsync(request);
    

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2014-01-24
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2014-05-23
      相关资源
      最近更新 更多