Implement Foursquare authentication in ASP.NET MVC

9 Dec


Here i will explain about how to implement Foursquare authentication in ASP.NET MVC. In order to use FoursquareAPI in our web application, first you need to create an application in  Foursquare Developer Page. The following are the steps to follow to create an application in Foursquare.

Step 1: Create your app to obtain its Foursquare API credentials



Once you app created, Client ID  the Client Secret   API credentials will be generated that we can use them for the authentication.

Step 2: Implementing Authentication

Create a login button with Foursquare logo, which will be found at Now we are using the OAuth 2.0 flow for the user authentication. We will direct users to the following url for login

If the user accepts, they will be redirected back to


Once we get the code, we will  exchange the code  for an access token. To get the access token we should make a request for the following url

Now we will use the above flow in ASP.NET MVC.

<a href="
<img src="~/Content/foursquare.png" />

Once the user authenticated use the following code to get the access token

 public ActionResult Index(string code)
    if (!string.IsNullOrEmpty(code))
        //Make a request for access token
        WebClient client = new WebClient();
        string reply =
                "" +

        //Deserialize the JSON to get the access token
        var jss = new JavaScriptSerializer();
        var dict = jss.Deserialize<Dictionary<string, string>>(reply);
        //Save the in a session for api endpoints calls
        Session["access_token"] = dict["access_token"];

    return View();

Save the access token in a session. Once you have an access token, it is easy use any of the endpoints, by just adding oauth_token=ACCESS_TOKEN to your GET or POST request.

Get the user profile using access token

//Read access_token from the session 
var acessToken = Session["access_token"];
                WebClient client = new WebClient();
                var reply =
                        "" + acessToken + "&v=20141112");

Leave a Reply

Your email address will not be published. Required fields are marked *