0

Hello everyone who can help me. I'm trying to do webmethods that makes call to a server. I want to ensure that data don't get mix with two different clients making the same call to the web method. If anyone got one example or anything that can help me will be great. If im not clear with the question please feel free to ask me.

Thanks!

Lachlan Dowding
  • 4,356
  • 1
  • 15
  • 18
Christian
  • 11
  • 3

1 Answers1

0

Assuming you have to call from the client to server:
You can insert unique ids to your calls using Math.random() based generators or libraries.

Or when you load the page, you can throw in the SessionId from ASP.NET (or retrieve it from cookies), and add that to your request.

So it would look like this:
Server:

[WebMethod]
public static string Hello(string name, string sessionId)
{
    return "Hello " + name;
}

Client:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript"></script>
<script type ="text/javascript">
    function guid() {
      function s4() {
          return Math.floor((1 + Math.random()) * 0x10000)
           .toString(16)
           .substring(1);
      }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
          s4() + '-' + s4() + s4() + s4();
    }
    $(document).ready(function () {
        $('#somebutton').click(function () {
            var id = 
            $.ajax({
                type: "POST",
                url: "SomeController/Hello",
                data: "{'name':'" + $('#nametxtbox').val() + "', 'sessionId':'" + guid() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(msg.d);
                }
            })
            return false;
        });
    });
</script>
Mithgroth
  • 1,114
  • 2
  • 13
  • 23
  • So I have to see the server side to confirm that it is correct? (pd: sorry im a rookie in this topic) – Christian Dec 04 '17 at 17:59
  • Since I don't know your client (windows form, wpf, asp.net, ?) I tried to give a general example. Generate a unique id (guid) in the client side, add that to your request. This way, you can have an opinion about the caller in the server side. – Mithgroth Dec 04 '17 at 20:26