Guest User

Untitled

a guest
Dec 19th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. OAuthWebSecurity.RegisterClient(new FacebookScopedClient("your_app_id", "your_app_secret"), "Facebook", null);
  2.  
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10.  
  11. public class FacebookScopedClient : IAuthenticationClient
  12. {
  13. private string appId;
  14. private string appSecret;
  15.  
  16. private const string baseUrl = "https://www.facebook.com/dialog/oauth?client_id=";
  17. public const string graphApiToken = "https://graph.facebook.com/oauth/access_token?";
  18. public const string graphApiMe = "https://graph.facebook.com/me?";
  19.  
  20.  
  21. private static string GetHTML(string URL)
  22. {
  23. string connectionString = URL;
  24.  
  25. try
  26. {
  27. System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(connectionString);
  28. myRequest.Credentials = CredentialCache.DefaultCredentials;
  29. //// Get the response
  30. WebResponse webResponse = myRequest.GetResponse();
  31. Stream respStream = webResponse.GetResponseStream();
  32. ////
  33. StreamReader ioStream = new StreamReader(respStream);
  34. string pageContent = ioStream.ReadToEnd();
  35. //// Close streams
  36. ioStream.Close();
  37. respStream.Close();
  38. return pageContent;
  39. }
  40. catch (Exception)
  41. {
  42. }
  43. return null;
  44. }
  45.  
  46. private IDictionary<string, string> GetUserData(string accessCode, string redirectURI)
  47. {
  48.  
  49. string token = GetHTML(graphApiToken + "client_id=" + appId + "&redirect_uri=" + HttpUtility.UrlEncode(redirectURI) + "&client_secret=" + appSecret + "&code=" + accessCode);
  50. if (token == null || token == "")
  51. {
  52. return null;
  53. }
  54. string data = GetHTML(graphApiMe + "fields=id,name,email,username,gender,link&access_token=" + token.Substring("access_token=", "&"));
  55.  
  56. // this dictionary must contains
  57. Dictionary<string, string> userData = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
  58. return userData;
  59. }
  60.  
  61. public FacebookScopedClient(string appId, string appSecret)
  62. {
  63. this.appId = appId;
  64. this.appSecret = appSecret;
  65. }
  66.  
  67. public string ProviderName
  68. {
  69. get { return "Facebook"; }
  70. }
  71.  
  72. public void RequestAuthentication(System.Web.HttpContextBase context, Uri returnUrl)
  73. {
  74. string url = baseUrl + appId + "&redirect_uri=" + HttpUtility.UrlEncode(returnUrl.ToString()) + "&scope=email";
  75. context.Response.Redirect(url);
  76. }
  77.  
  78. public AuthenticationResult VerifyAuthentication(System.Web.HttpContextBase context)
  79. {
  80. string code = context.Request.QueryString["code"];
  81.  
  82. string rawUrl = context.Request.Url.OriginalString;
  83. //From this we need to remove code portion
  84. rawUrl = Regex.Replace(rawUrl, "&code=[^&]*", "");
  85.  
  86. IDictionary<string, string> userData = GetUserData(code, rawUrl);
  87.  
  88. if (userData == null)
  89. return new AuthenticationResult(false, ProviderName, null, null, null);
  90.  
  91. string id = userData["id"];
  92. string username = userData["username"];
  93. userData.Remove("id");
  94. userData.Remove("username");
  95.  
  96. AuthenticationResult result = new AuthenticationResult(true, ProviderName, id, username, userData);
  97. return result;
  98. }
  99. }
  100.  
  101. public ActionResult ExternalLoginCallback(string returnUrl)
  102.  
  103. public static class String
  104. {
  105. public static string Substring(this string str, string StartString, string EndString)
  106. {
  107. if (str.Contains(StartString))
  108. {
  109. int iStart = str.IndexOf(StartString) + StartString.Length;
  110. int iEnd = str.IndexOf(EndString, iStart);
  111. return str.Substring(iStart, (iEnd - iStart));
  112. }
  113. return null;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment