阅读 OAuth2.0 Signed_Request Facebook 注册 C# MVC

发布于 2024-10-09 19:29:48 字数 859 浏览 0 评论 0原文

我的问题非常相似这个 但我想我需要更进一步。

Facebook 表示,“数据作为签名请求传递到您的应用程序。signed_request 参数是确保您收到的数据是 Facebook 发送的实际数据的简单方法。”

用户登录我的 asp c# MVC 站点并单击“注册”后,重定向 URL 为 http://site/帐户/注册。那时(发布到帐户/注册控件),我想使用签名的请求收集用户的信息,以便我可以在本地站点上注册它们。我不知道如何访问 Facebook 提供的数据。

$data = json_decode(base64_url_decode($payload), true);

C# 中的等价物是什么? Facebook 在帖子中传递什么类型的变量/数据?我如何访问“$payload”?

[HttpPost]
    public ActionResult RegisterFacebook(RegisterFacebookModel model)
    {
        Facebook.FacebookSignedRequest sr = Facebook.FacebookSignedRequest.Parse("secret", model.signed_request);

        return View(model);
    }

My question is very similar this but I guess I need to take it one step further.

Facebook says "The data is passed to your application as a signed request. The signed_request parameter is a simple way to make sure that the data you're receiving is the actual data sent by Facebook."

After a user has logged into my asp c# MVC site and clicked "Register", the redirect-url is http://site/account/register. At that point (the post to the account/register control), I would like to gather the user's information using the signed request so that I can register them with my site locally. I cannot figure out how to access the data facebook makes available.

$data = json_decode(base64_url_decode($payload), true);

What is the equivalent in C#? What type of variable/data is facebook passing in the post? And how do I access "$payload"?

[HttpPost]
    public ActionResult RegisterFacebook(RegisterFacebookModel model)
    {
        Facebook.FacebookSignedRequest sr = Facebook.FacebookSignedRequest.Parse("secret", model.signed_request);

        return View(model);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清泪尽 2024-10-16 19:29:48

这是我们在 Facebook C# SDK 中使用的代码。如果您使用我们的 sdk,则不需要手动执行此操作,但如果您需要自己执行此操作,则为:

/// <summary>
/// Parses the signed request string.
/// </summary>
/// <param name="signedRequestValue">The encoded signed request value.</param>
/// <returns>The valid signed request.</returns>
internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)
{
    Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
    Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

    string[] parts = signedRequestValue.Split('.');
    var encodedValue = parts[0];
    if (String.IsNullOrEmpty(encodedValue))
    {
        throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
    }

    var sig = Base64UrlDecode(encodedValue);
    var payload = parts[1];

    using (var cryto = new System.Security.Cryptography.HMACSHA256(Encoding.UTF8.GetBytes(this.AppSecret)))
    {
        var hash = Convert.ToBase64String(cryto.ComputeHash(Encoding.UTF8.GetBytes(payload)));
        var hashDecoded = Base64UrlDecode(hash);
        if (hashDecoded != sig)
        {
            return null;
        }
    }

    var payloadJson = Encoding.UTF8.GetString(Convert.FromBase64String(Base64UrlDecode(payload)));
    var data = (IDictionary<string, object>)JsonSerializer.DeserializeObject(payloadJson);
    var signedRequest = new FacebookSignedRequest();
    foreach (var keyValue in data)
    {
        signedRequest.Dictionary.Add(keyValue.Key, keyValue.Value.ToString());
    }

    return signedRequest;
}

/// <summary>
/// Converts the base 64 url encoded string to standard base 64 encoding.
/// </summary>
/// <param name="encodedValue">The encoded value.</param>
/// <returns>The base 64 string.</returns>
private static string Base64UrlDecode(string encodedValue)
{
    Contract.Requires(!String.IsNullOrEmpty(encodedValue));

    encodedValue = encodedValue.Replace('+', '-').Replace('/', '_').Trim();
    int pad = encodedValue.Length % 4;
    if (pad > 0)
    {
        pad = 4 - pad;
    }

    encodedValue = encodedValue.PadRight(encodedValue.Length + pad, '=');
    return encodedValue;
}

您可以在此处找到完整的源代码:http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs

Here is the code we used in the Facebook C# SDK. You don't need to do this manually if you use our sdk, but if you need to do it yourself here it is:

/// <summary>
/// Parses the signed request string.
/// </summary>
/// <param name="signedRequestValue">The encoded signed request value.</param>
/// <returns>The valid signed request.</returns>
internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)
{
    Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
    Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

    string[] parts = signedRequestValue.Split('.');
    var encodedValue = parts[0];
    if (String.IsNullOrEmpty(encodedValue))
    {
        throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
    }

    var sig = Base64UrlDecode(encodedValue);
    var payload = parts[1];

    using (var cryto = new System.Security.Cryptography.HMACSHA256(Encoding.UTF8.GetBytes(this.AppSecret)))
    {
        var hash = Convert.ToBase64String(cryto.ComputeHash(Encoding.UTF8.GetBytes(payload)));
        var hashDecoded = Base64UrlDecode(hash);
        if (hashDecoded != sig)
        {
            return null;
        }
    }

    var payloadJson = Encoding.UTF8.GetString(Convert.FromBase64String(Base64UrlDecode(payload)));
    var data = (IDictionary<string, object>)JsonSerializer.DeserializeObject(payloadJson);
    var signedRequest = new FacebookSignedRequest();
    foreach (var keyValue in data)
    {
        signedRequest.Dictionary.Add(keyValue.Key, keyValue.Value.ToString());
    }

    return signedRequest;
}

/// <summary>
/// Converts the base 64 url encoded string to standard base 64 encoding.
/// </summary>
/// <param name="encodedValue">The encoded value.</param>
/// <returns>The base 64 string.</returns>
private static string Base64UrlDecode(string encodedValue)
{
    Contract.Requires(!String.IsNullOrEmpty(encodedValue));

    encodedValue = encodedValue.Replace('+', '-').Replace('/', '_').Trim();
    int pad = encodedValue.Length % 4;
    if (pad > 0)
    {
        pad = 4 - pad;
    }

    encodedValue = encodedValue.PadRight(encodedValue.Length + pad, '=');
    return encodedValue;
}

You can find the full source code here: http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs

笙痞 2024-10-16 19:29:48

根据您的评论,您似乎仍在寻找 FB 发送的回复。我相信它包含在 HttpContext Request 对象的 Form 集合中。因此,从您指定为重定向的页面中,您应该能够从以下位置提取它:

HttpContext.Current.Request.Form("signed_request")

希望这有助于阐明一些内容。我仍在不断学习,所以这可能不是最好的解决方案。

谢谢,
贾森

Based on your comment, it looks like you're still looking for the response that FB is sending. I believe it it contained in the Form collection in the HttpContext Request object. So from the page you specify as the redirect, you should be able to pull it from:

HttpContext.Current.Request.Form("signed_request")

Hope that helps shed some light. I'm still learning as I go so this may not be the best solution.

thanks,
Jason

装迷糊 2024-10-16 19:29:48

以下是如何使用 Facebook SDK 执行此操作

var parsedSignedRequest = FacebookSignedRequest.Parse(FacebookApplication.Current, signed_request);

Here's how to do it using Facebook SDK

var parsedSignedRequest = FacebookSignedRequest.Parse(FacebookApplication.Current, signed_request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文