Authentication failing with multiple threads c#

偶尔善良 提交于 2019-12-08 08:24:24

问题


I'm currently writing a web application that provides some database manipulation. The system also needs to be capable working with different databases.The problem is that certain fields in the database have to be updated with data from a RESTful api. To perform these updates, I use a Quartz.NET schedule that makes a job for every table that needs to be updated.This job checks if it needs to update the relevant database. If it does, it gets the information from the REST-service. When I don't use multithreading, there are no problems with accessing the REST-serivce. But when I use Quartz.net, the server returns a 401 error for some requests. This behavior is inconstistent and doesn't always happen. I'm using RestSharper to make these calls with a custom Authenticator for digest authentication. I got this solution here:
http://www.ifjeffcandoit.com/2013/05/16/digest-authentication-with-restsharp/

I also use the auth fixer in that post.

Creating the jobs:

            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);

The response in this piece of code receives the 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);

I looked at the request with fiddler, but didn't really see anything usefull. Any help or tips would be very welcome. Thanks in advance!


回答1:


Not entirely sure how this fixed it, but all authentication seems to work by disabling the use of the SyncronisationContext and making the method wait for the task.

  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);


来源:https://stackoverflow.com/questions/29211964/authentication-failing-with-multiple-threads-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!