发送HTTP标头后,DotNetOpenAuth授权无法设置状态?

时间:2012-11-12 17:49:30

标签: c# asp.net-mvc facebook dotnetopenauth

我的elmah错误日志填写异常:

在发送HTTP标头后,服务器无法设置状态。

这些请求中有70%是由抓取工具(alexa,googlebot)引起的,我不能保证其余的请求不是带有假cookie,代理字符串等的机器人,但其中一些请求看起来确实合法。

以下是出现错误的Facebook授权方法:

  public class FacebookClient : WebServerClient 
    {
        private static readonly AuthorizationServerDescription FacebookDescription = new AuthorizationServerDescription 
        {
            TokenEndpoint = new Uri("https://graph.facebook.com/oauth/access_token"),
            AuthorizationEndpoint = new Uri("https://graph.facebook.com/oauth/authorize"),
        };

        /// <summary>
        /// Initializes a new instance of the <see cref="FacebookClient"/> class.
        /// </summary>
        public FacebookClient() : base(FacebookDescription) 
        {
        }
    }

 private static readonly FacebookClient client = new FacebookClient
    {
        ClientIdentifier = ConfigurationManager.AppSettings["facebookAppID"],

        ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["facebookAppSecret"]),
    };

    [AllowAnonymous]
    public ActionResult Facebook(string returnUrl)
    {
        IAuthorizationState authorization = client.ProcessUserAuthorization();

        if (authorization == null)
        {
            var scope = new List<string>();

            scope.Add("email");

            client.RequestUserAuthorization(scope);
        }
        else
        {
            try
            {
                var request = WebRequest.Create("https://graph.facebook.com/me?&access_token=" + Uri.EscapeDataString(authorization.AccessToken));

                using (var response = request.GetResponse())

                using (var responseStream = response.GetResponseStream())
                {
                    var graph = FacebookGraph.Deserialize(responseStream);

                    if (Membership.GetUser(graph.Id.ToString()) == null)
                    {
                        MembershipCreateStatus membershipCreateStatus = MembershipCreateStatus.Success;

                        var user = Common.CreateUser(membershipCreateStatus, graph.Id.ToString(), HttpUtility.HtmlEncode(graph.Email));

                        if (membershipCreateStatus != MembershipCreateStatus.Success)
                        {
                            TempData["message"] = "Unsuccessful creation of Account. " + membershipCreateStatus.ToString();

                            return RedirectToAction("Login", "Account");
                        }

                        if (membershipCreateStatus == MembershipCreateStatus.Success)
                        {

                            AddUserShortID((Guid)user.ProviderUserKey, HttpUtility.HtmlEncode(graph.Name));

                            Common.Authorize(graph.Id.ToString());
                        }
                    }
                    else
                    {
                        Common.Authorize(graph.Id.ToString());
                    }
                }
            }
            catch
            {
                TempData["message"] = "Unsuccessful creation of Account. ";

                return RedirectToAction("Login", "Account");
            }
        }

        if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }

        return RedirectToAction("Index", "Home");
    }

这里可能导致此类行为的代码中最可疑的部分是什么?为什么这种情况大部分时间都发生在机器人身上?有没有办法重现这样的东西?

更新:这不仅仅是因为机器人我昨天也得到了这个例外,只有在日志中,在浏览器中我找不到graph.facebook.com /...

1 个答案:

答案 0 :(得分:1)

我发现当这个异常发生时,当我被重定向到Facebook登录页面时,如果你刷新页面或输入错误的登录信息,或者如果页面当前不可用,无论出于什么原因记录此异常,但是最重要的是,用户不会发现任何异常情况,或登录时遇到任何问题。