Created
January 2, 2015 17:53
-
-
Save mvark/bc8ba919c41e55c7c878 to your computer and use it in GitHub Desktop.
Authentication & Authorization using OAuth 2.0 Providers with ASP.NET WebForms: Code from Blaize Stewart's video tutorial "OAuth 2.0 Up and Running" (requires subscription), modified to include Microsoft's Live Connect functionality. See related blog posting - http://mvark.blogspot.in/2014/01/how-to-implement-authentication.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
OAuthRedirectPage.aspx | |
========================== | |
<div> | |
<asp:Label ID="OAuthLabel" runat="server" Text="Label"></asp:Label> | |
</div> | |
OAuthRedirectPage.aspx.cs | |
========================== | |
*/ | |
using System; | |
using Newtonsoft.Json.Linq; | |
public partial class OAuthRedirectPage : System.Web.UI.Page | |
{ | |
string OAuthURL = "https://accounts.google.com/o/oauth2/auth"; | |
string OAuthTokenURL = "https://accounts.google.com/o/oauth2/token"; | |
string client_id = "your_app_client_id"; | |
string client_secret = "your_app_client_secret"; | |
string scope = "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"; | |
string redirect_uri = "http://example.com/OAuthRedirectPage.aspx"; | |
string serviceURL = "https://www.googleapis.com/oauth2/v3/userinfo"; | |
//string OAuthURL = "https://www.facebook.com/dialog/oauth"; | |
//string OAuthTokenURL = "https://graph.facebook.com/oauth/access_token"; | |
//string client_id = "your_app_client_id"; | |
//string client_secret = "your_app_client_secret"; | |
//string scope = "user_about_me"; | |
//string redirect_uri = "http://example.com/OAuthRedirectPage.aspx"; | |
//string serviceURL = "https://graph.facebook.com/me"; | |
//string OAuthURL = "https://login.live.com/oauth20_authorize.srf"; | |
//string OAuthTokenURL = "https://login.live.com/oauth20_token.srf"; | |
//string client_id = "your_app_client_id"; | |
//string client_secret = "your_app_client_secret"; | |
//string scope = "wl.basic"; | |
//string redirect_uri = "http://example.com/OAuthRedirectPage.aspx"; | |
//string serviceURL = "https://apis.live.net/v5.0/me"; | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
OAuthLabel.Text = "OAuth Redirect Page"; | |
if (Request["code"] != null) | |
{ | |
//Build the form request from the parameters | |
string formData="client_id=" + client_id + | |
"&client_secret=" + client_secret + | |
"&redirect_uri=" + redirect_uri + | |
"&grant_type=authorization_code" + | |
"&code=" + Request["code"]; | |
//Exchange code for access token | |
System.Net.WebClient ExchangeWC = new System.Net.WebClient(); | |
ExchangeWC.Headers.Add("Content-Type","application/x-www-form-urlencoded"); | |
var Results = ExchangeWC.UploadString(new System.Uri(OAuthTokenURL), formData); | |
//Extract token from the results | |
string access_token = ""; | |
try | |
{ | |
JObject TokenData = JObject.Parse(Results); | |
access_token = TokenData["access_token"].ToString(); | |
} | |
catch (Exception) //next try URL encoded data | |
{ | |
string[] URLParts = Results.Split('&'); | |
foreach (string S in URLParts) //extract the code from the URL | |
{ | |
string[] param = S.Split('='); | |
if (param[0].Replace("?", "") == "access_token") | |
{ | |
access_token = param[1]; | |
break; | |
} | |
} | |
} | |
//Call a service with the token | |
System.Net.WebClient ProfileWC = new System.Net.WebClient(); | |
ProfileWC.Headers.Add("Authorization", "Bearer " + access_token); | |
Results = ProfileWC.DownloadString(new System.Uri(serviceURL)); | |
//Display the users name.. | |
JObject UserProfile = JObject.Parse(Results); | |
OAuthLabel.Text = "Hello, " + UserProfile["name"].ToString() ; | |
} | |
else //no "code" detected, redirect to OAuth service | |
{ | |
string URL = OAuthURL + "" + | |
"?client_id=" + client_id + | |
"&scope=" + scope + | |
"&redirect_uri=" + redirect_uri + | |
"&response_type=code"; | |
Response.Redirect(URL); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment