Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, the extraData parameter is not passed to facebook. It is for internal use only. See the following link on how this data can be used on your site:</p> <p><a href="http://blogs.msdn.com/b/pranav_rastogi/archive/2012/08/24/customizing-the-login-ui-when-using-oauth-openid.aspx" rel="nofollow">http://blogs.msdn.com/b/pranav_rastogi/archive/2012/08/24/customizing-the-login-ui-when-using-oauth-openid.aspx</a></p> <p>Now, to the meat:</p> <p>In addition to the methods <code>RegisterFacebookClient</code>, <code>RegisterYahooClient</code> etc. in <code>OAuthWebSecurity</code>, there is also a generic method <code>RegisterClient</code>. This is the method we will be using for this solution.</p> <p>This idea germinates from the code provided at: <a href="http://mvc4beginner.com/Sample-Code/Facebook-Twitter/MVC-4-oAuth-Facebook-Login-EMail-Problem-Solved.html" rel="nofollow">http://mvc4beginner.com/Sample-Code/Facebook-Twitter/MVC-4-oAuth-Facebook-Login-EMail-Problem-Solved.html</a></p> <p>However, we will not be using the hacky approach provided by the solution. Instead, we will create a new class called <code>FacebookScopedClient</code> which will implement <code>IAuthenticationClient</code>. Then we will simply register the class using:</p> <pre><code>OAuthWebSecurity.RegisterClient(new FacebookScopedClient("your_app_id", "your_app_secret"), "Facebook", null); </code></pre> <p>in AuthConfig.cs</p> <p>The code for the class is:</p> <pre><code>using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web; public class FacebookScopedClient : IAuthenticationClient { private string appId; private string appSecret; private const string baseUrl = "https://www.facebook.com/dialog/oauth?client_id="; public const string graphApiToken = "https://graph.facebook.com/oauth/access_token?"; public const string graphApiMe = "https://graph.facebook.com/me?"; private static string GetHTML(string URL) { string connectionString = URL; try { System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(connectionString); myRequest.Credentials = CredentialCache.DefaultCredentials; //// Get the response WebResponse webResponse = myRequest.GetResponse(); Stream respStream = webResponse.GetResponseStream(); //// StreamReader ioStream = new StreamReader(respStream); string pageContent = ioStream.ReadToEnd(); //// Close streams ioStream.Close(); respStream.Close(); return pageContent; } catch (Exception) { } return null; } private IDictionary&lt;string, string&gt; GetUserData(string accessCode, string redirectURI) { string token = GetHTML(graphApiToken + "client_id=" + appId + "&amp;redirect_uri=" + HttpUtility.UrlEncode(redirectURI) + "&amp;client_secret=" + appSecret + "&amp;code=" + accessCode); if (token == null || token == "") { return null; } string data = GetHTML(graphApiMe + "fields=id,name,email,gender,link&amp;access_token=" + token.Substring("access_token=", "&amp;")); // this dictionary must contains Dictionary&lt;string, string&gt; userData = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, string&gt;&gt;(data); return userData; } public FacebookScopedClient(string appId, string appSecret) { this.appId = appId; this.appSecret = appSecret; } public string ProviderName { get { return "Facebook"; } } public void RequestAuthentication(System.Web.HttpContextBase context, Uri returnUrl) { string url = baseUrl + appId + "&amp;redirect_uri=" + HttpUtility.UrlEncode(returnUrl.ToString()) + "&amp;scope=email"; context.Response.Redirect(url); } public AuthenticationResult VerifyAuthentication(System.Web.HttpContextBase context) { string code = context.Request.QueryString["code"]; string rawUrl = context.Request.Url.OriginalString; //From this we need to remove code portion rawUrl = Regex.Replace(rawUrl, "&amp;code=[^&amp;]*", ""); IDictionary&lt;string, string&gt; userData = GetUserData(code, rawUrl); if (userData == null) return new AuthenticationResult(false, ProviderName, null, null, null); string id = userData["id"]; string username = userData["email"]; userData.Remove("id"); userData.Remove("email"); AuthenticationResult result = new AuthenticationResult(true, ProviderName, id, username, userData); return result; } } </code></pre> <p>now in the </p> <pre><code>public ActionResult ExternalLoginCallback(string returnUrl) </code></pre> <p>method in <code>AccountController</code>, <code>result.ExtraData</code> should have the email.</p> <p><strong>Edit: I missed some code in this post. I am adding it below:</strong></p> <pre><code>public static class String { public static string Substring(this string str, string StartString, string EndString) { if (str.Contains(StartString)) { int iStart = str.IndexOf(StartString) + StartString.Length; int iEnd = str.IndexOf(EndString, iStart); return str.Substring(iStart, (iEnd - iStart)); } return null; } } </code></pre> <p>Cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload