mercredi 1 août 2018

How can I avoid invalid session issue if I got url for request on back-end in one country and use it on the client-side in an other one?

I have an asp mvc app that provide some sandbox to developers to check theirs SDKs. It's pretty simple - I have a few endpoints to check, e.g.: india.myapp.com, england.myapp.com, usa.myapp.com (they are located on aws-instances in india, england and america and has different back-ends, not linked one to another). Well, if you want to check how your SDK works for indian-user, u can go to this endpoint and just click one button (app should create new window with some specific for the current region (endpoint's country) data, e.g. country's postcode etc). So earlier I had realization when all the requests exetutes on the client side and responses contained data which was specific for client's region, except of endpoint's region. So now I'm trying to fix it by pass requests to back-end-side. Now I have something like this:

Click on button calls "/Controller/TestMethod" controller's method:

$('#livetest')
                .click(function () {
                    var tst = '/Controller/TestMethod';
                    var endpoint = $('input[name=endpoint]:checked').val();
                    var id = $('input[name=ClientId]').val();
                   var secret = $('input[name=ClientSecret]').val();
                   var redirect = $('input[name=RedirectUrl]').val();

                   $.get(tst,
                       { type: endpoint, id: id, secret: secret, rurl: redirect },
                       function (data) {
                           openWindow(data);
                        });

               });

Controller's method "TestMethod":

[HttpGet]
        public string TestMethod(string type, string id, string secret, string rurl)
        {
            return GetPageUrl(Configuration.GetEndpointsConfig()[type].Url, id, secret, rurl);
        }

        private static string GetPageUrl(string url, string id, string secret, string rurl)
        {
            var resultUrl = $"{url}?Redirect_URL={rurl}";
            var request = (HttpWebRequest)WebRequest.Create(resultUrl);
            request.AllowAutoRedirect = false;
            var creds = $"{id}:{secret}";
            var credsx = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(creds));
            request.Headers[HttpRequestHeader.Authorization] = credsx;

            var result = (HttpWebResponse)request.GetResponse();
            return result.Headers[HttpResponseHeader.Location];
        }

And with this code I get session-error (there isn't any errors in console, but instead of the data I see "invalid session") when locations of client and server are different (e.g. developer from USA has USA IP and current endpoint is England has england IP).

So I'm quite newbie in asp and as far as I see there are two ways to avoid this issue:

  1. try to create new window from asp.code . But I found solutions only for web-forms, not for mvc (like onClick events).
  2. I found that tag-helpers can execute JS on the server side (if I got it right) so maybe it'll can be a good solution.
  3. find a way to pass some location headers or smth like that.
  4. create a proxy for all requests through back-end (using c#, because on aws-intance I don't have any balancers or smth like that, only IIS).

so I'm looking for the simpliest solution (or best practices) and thanks in advance for any help.




Aucun commentaire:

Enregistrer un commentaire