为什么2017年3月之后不登录Facebook登录?

时间:2017-04-27 13:57:36

标签: facebook asp.net-mvc-4 oauth

我在MVC项目中使用Oauth进行Facebook授权约1年,但效果很好,但突然间它停止了工作。在游行之后的这一刻,它不起作用。当我写用户并传递授权页面时,它会抛出空白页面。有什么变化或者是什么?

以下是我网站的授权页面:http://solve.ge/Account/Login

请在此处查看我的代码:

#region Constructors and Destructors

/// <summary>
/// Initializes a new instance of the <see cref="FacebookClient"/> class.
/// </summary>
/// <param name="appId">
/// The app id.
/// </param>
/// <param name="appSecret">
/// The app secret.
/// </param>
public FacebookClient(string appId, string appSecret)
    : base("facebook")
{
    if (appId == null)
        throw new ArgumentNullException("appId");
    if (appSecret == null)
        throw new ArgumentNullException("appSecret");
    this.appId = appId;
    this.appSecret = appSecret;
}

#endregion

#region Methods

/// <summary>
/// The get service login url.
/// </summary>
/// <param name="returnUrl">
/// The return url.
/// </param>
/// <returns>An absolute URI.</returns>
protected override Uri GetServiceLoginUrl(Uri returnUrl)
{
    // Note: Facebook doesn't like us to url-encode the redirect_uri value
    returnUrl = OauthReWriteRequest.ChangeHost(returnUrl);
    UriBuilder uriBuilder = new UriBuilder(AuthorizationEndpoint);
    uriBuilder.AppendQueryArgument("client_id", this.appId);
    uriBuilder.AppendQueryArgument("redirect_uri", returnUrl.GetLeftPart(UriPartial.Path));
    uriBuilder.AppendQueryArgument("response_type", "code");
    uriBuilder.AppendQueryArgument("scope", "user_photos,email,user_birthday");//AuthorizationEndpoint);
    uriBuilder.AppendQueryArgument("state", returnUrl.Query.Substring(1));

    return uriBuilder.Uri;
}

/// <summary>
/// The get user data.
/// </summary>
/// <param name="accessToken">
/// The access token.
/// </param>
/// <returns>A dictionary of profile data.</returns>
protected override IDictionary<string, string> GetUserData(string accessToken)
{
    FacebookGraphData graphData;
    var request =
        WebRequest.Create(
            "https://graph.facebook.com/me?access_token=" + accessToken);//MessagingUtilities.EscapeUriDataStringRfc3986(accessToken));
    using (var response = request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            string rawJson = new StreamReader(responseStream).ReadToEnd();
            graphData = JsonDeserialize<FacebookGraphData>(rawJson);
        }
    }

    // this dictionary must contains 
    var userData = new Dictionary<string, string>();
    if (!graphData.Id.IsNullOrWhiteSpace())
    {
        userData.Add("id", graphData.Id);
    }
    if (!graphData.Email.IsNullOrWhiteSpace())
    {
        userData.Add("username", graphData.Email);
    }
    if (!graphData.Name.IsNullOrWhiteSpace())
    {
        userData.Add("name", graphData.Name);
    }
    if (!graphData.Gender.IsNullOrWhiteSpace())
    {
        userData.Add("gender", graphData.Gender);
    }
    if (!graphData.Birthday.IsNullOrWhiteSpace())
    {
        userData.Add("birthday", graphData.Birthday);
    }
    userData.Add("link", graphData.Link == null ? null : graphData.Link.AbsoluteUri);

    return userData;
}

/// <summary>
/// Obtains an access token given an authorization code and callback URL.
/// </summary>
/// <param name="returnUrl">
/// The return url.
/// </param>
/// <param name="authorizationCode">
/// The authorization code.
/// </param>
/// <returns>
/// The access token.
/// </returns>
protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
{
    returnUrl = OauthReWriteRequest.ChangeHost(returnUrl);
    // Note: Facebook doesn't like us to url-encode the redirect_uri value
    var builder = new UriBuilder("https://graph.facebook.com/oauth/access_token");
    builder.AppendQueryArgument("client_id", this.appId);
    builder.AppendQueryArgument("redirect_uri", NormalizeHexEncoding(returnUrl.GetLeftPart(UriPartial.Path)));
    builder.AppendQueryArgument("client_secret", this.appSecret);
    builder.AppendQueryArgument("code", authorizationCode);

    using (WebClient client = new WebClient())
    {
        //Get Accsess  Token
        string data = client.DownloadString(builder.Uri);

        if (string.IsNullOrEmpty(data))
        {
            return null;
        }

        var parsedQueryString = HttpUtility.ParseQueryString(data);
        return parsedQueryString["access_token"];
    }
}

/// <summary>
/// Converts any % encoded values in the URL to uppercase.
/// </summary>
/// <param name="url">The URL string to normalize</param>
/// <returns>The normalized url</returns>
/// <example>NormalizeHexEncoding("Login.aspx?ReturnUrl=%2fAccount%2fManage.aspx") returns "Login.aspx?ReturnUrl=%2FAccount%2FManage.aspx"</example>
/// <remarks>
/// There is an issue in Facebook whereby it will rejects the redirect_uri value if
/// the url contains lowercase % encoded values.
/// </remarks>
private static string NormalizeHexEncoding(string url)
{
    var chars = url.ToCharArray();
    for (int i = 0; i < chars.Length - 2; i++)
    {
        if (chars[i] == '%')
        {
            chars[i + 1] = char.ToUpperInvariant(chars[i + 1]);
            chars[i + 2] = char.ToUpperInvariant(chars[i + 2]);
            i += 2;
        }
    }
    return new string(chars);
}


#endregion

#region Json Serialization And Deserialization
/// <summary>
/// JSON Serialization
/// </summary>
public static string JsonSerializer<T>(T t)
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream();
    ser.WriteObject(ms, t);
    string jsonString = Encoding.UTF8.GetString(ms.ToArray());
    ms.Close();
    return jsonString;
}
/// <summary>
/// JSON Deserialization
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
    T obj = (T)ser.ReadObject(ms);
    return obj;
}
#endregion Json Serialization And Deserialization

非常感谢

0 个答案:

没有答案