ASP.NET Identity with Facebook (out of the box)

The following is a walk through of how to deal with authentication using Identity Providers mainly Facebook.

Open https://developers.facebook.com/

Add a New App
Add a New App

Click “Add a New App”

 

Add a New App
Add a New App

Click “WWW”

Create New Facebook App ID
Create New Facebook App ID

Click “Create New Facebook App ID”

Create New app ID/Details
Create New app ID/Details

Select Category and click “Create App ID”

Go to Visual studio 2013

New VS2013 Project
New VS2013 Project

Create new Project name it and select “ASP.NET Web application”

New VS2013 Project Selection
New VS2013 Project Selection

Set  Authentication to “Individual User Accounts” and Click Ok

Build and run your project and copy the local URL go to Facebook Development in your browser where you left off

Website URL
Website URL

Paste the url in “Site URL” and “Mobile site URL” in this case localhost:61617 and click next

 

Application DashBoard
Application DashBoard

Refresh the page click Show button near App Secret

 

Authentication
Authentication

Enter your Facebook Password

Go to visual studio

Startup Authentication
Startup Authentication

Navigate in solution explorer to App_start/start.auth.cs

Facebook App ID and App Secret
Facebook App ID and App Secret

Scroll down Facebook boilerplate authentication code. Paste App Id and App Secret and uncomment the three lines

Rebuild the Project and run

Log In
Log In

Once it opens in the browser click Login

Facebook Login
Facebook Login

On the left of the page there should be a Facebook button click it

Application Authorization
Application Authorization

Click okay to authorize

Associate E-mail address with account
Associate E-mail address with account

Enter E-mail address to associate with that Facebook account on your application

Display E-mail after authentication
Display E-mail after authentication

The e-mail will show every time the user is logged in.

That is how to authenticate a user using Facebook as an Identity Provider. Out of the box.

 

What’s else can we do with Facebook?

There a lot of things one can do with Facebook by using Facebook SDK the user can authorize your Web Site to access several Scopes. At the least one could skip the last two steps by requesting access to the e-mail and use that programmatically without the user having to type it.

The following is how to incorporate Facebook SDK in your project.

NuGet Manager
NuGet Manager
Facebook SDK
Facebook SDK

More info at http://www.nuget.org/packages/Facebook/7.0.5-beta

and https://developers.facebook.com/docs/graph-api

The guideline is do not ask for more data than your application really need, because once you lose trust of the users, your web site is dead.

 

.NET HTTPRequest

HTTP Handlers
They are used to process individual endpoints.
Only one handler per request is used
Can be both asynchronous or synchronous
Earliest possible point where you have access to requests

 

HTTPHandlers are extension based. This means that for every extension .aspx .html etc there is a handler (some share handler).

The example used here is very simple and does nothing special except for displaying some static text. The reason is that ideally before attempting to do something elaborate one should check if it works by doing something simple especially while learning.

The examples here were created in ASP.MVC5 applications. The extension that is used is .kk

Synchronous HTTP Handler

The Solution name is HTTP_Handler

Step 1:

Ignore routing for .kk extension in App_Start/RouteConfig.cs (The reason is that we do not want MVC to try to interpret this file)

routes.IgnoreRoute("{file}.kk");

Step 2:

We create the class that will be our handler at the top level  since this is an example and name it CustomHandler.cs

This can be achieved by inheriting IHttpHandler or by using IHttpHandlerFactory (The example uses IHttpHandler)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HTTP_Handler
{
public class CustomHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.Write("test");
}

public bool IsReusable
{
get { return true; }
}
}
}

Step 3:

We configure the application to use the handler (Integrated mode).

In web.config

<system.webServer>

<handlers>

<add verb="*" path="*.kk" name="CustomHandler" type="HTTP_Handler.CustomHandler"/>

</handlers>

Step 4:

Build and Run

add hello.kk to the link and load it should print test on screen.

 Asynchronous HTTP Handler

 

The Solution name is HTTP_Async_Handler

Step 1:

Ignore routing for .kk extension in App_Start/RouteConfig.cs (The reason is that we do not want MVC to try to interpret this file)

routes.IgnoreRoute("{file}.kk");

Step 2:

We create the class that will be our handler at the top level  since this is an example and name it CustomHandler.cs

This can be achieved by inheriting IHttpAsyncHandler

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HTTP_Async_Handler
{
public class CustomHandler:IHttpAsyncHandler
{
// Long Process
private static void generateNumbers(HttpContext context)
{
string s = "";
for (int i = 0; i < 1000; i++)
{
s = s + ' ' + i.ToString();
}
context.Response.Write(s);
}

public static Action<HttpContext> _Handle = generateNumbers;

public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Write("BeginProcessRequest Executed");
return _Handle.BeginInvoke(context,cb,extraData);
}

public void EndProcessRequest(IAsyncResult result)
{
// Clean up code here
}

public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write("ProcessRequest Executed Will not execute");
}
}
}

Step 3:

We configure the application to use the handler (Integrated mode).

In web.config

<system.webServer>

<handlers>

<add verb="*" path="*.kk" name="CustomHandler" type="HTTP_Handler.CustomHandler"/>

</handlers>

</system.webServer>

Step 4:

Build and Run

add hello.kk to the link and load it should print test on screen.

 

Want to know more?

Investigate IHttpHandlerFactory

Classic mode & Integrated mode (IIS)