Cannot read fraqment parameters from url

585 views Asked by At

I am working on integration with vk.com. Their documentation says that you will be redirected after authorization to next url

http://REDIRECT_URI#access_token= 533bacf01e11f55b536a565b57531ad114461ae8736d6506a3&expires_in=86400&user_id=8492 

And redirect happens but I don't see access_token, expires_in and user_id variables. I investigated HttpContext and didn't find these parameters. This HttpContext.Request.Url just shows me my url without #access_token= 533bacf01e11f55b536a565b57531ad114461ae8736d6506a3&expires_in=86400&user_id=8492

But if I replace # character with ? in url it works as expected.

I am working with vk api from ASP.NET MVC. Does anybody know how to get these parameters?

2

There are 2 answers

0
Petr Abdulin On BEST ANSWER

It's not possible to read fragment since it's not send to server (and VK probably do this for improved security of auth process) your option is to use JavaScript:

<script>
  if(window.location.hash) {
      //Puts hash in variable, and removes the # character
      var hash = window.location.hash.substring(1); 
      // hash found
      alert (hash);
  } else {
      // No hash found
  }
</script>
6
giammin On

Sadly the Fragment is not passed to the server.

It will always be empty when you attempt to get it from a Request.Uri.Fragment.

There is no easy way to get the fragment on the server. You have to use javascript to retrieve the fragment and send it to the server maybe with AJAX or you can create a middle page that read fragment and redirect the user to another page with the fragment replaced with querystrings.

The middle page could be something like:

if(window.location.hash) {
    window.location= '/yoururl.htm' + window.location.hash.replace('#','?');
}