Googleplus身份验证:OAuth 2参数只能包含一个值:代码

时间:2016-07-18 08:00:19

标签: c# oauth2

我正在尝试使用Oauth2在我的网站上实施Googleplus登录。 我想传递状态参数,该参数将在用户成功通过身份验证后使用。 我得到的错误是“OAuth 2参数只能有一个值:代码”。 如果我不使用state参数,它工作正常并验证用户。 以下是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        string returnPath = Request.QueryString["ReturnUrl"];
        if (returnPath == null)
        {
            returnPath = "ManageAccount.aspx";
        }
        else
        {
            returnPath = Request.QueryString["ReturnUrl"].ToString();
        }
        Session["state"] = returnPath;
            if (Session["Provider"] != null)
            {
                if (Session["Provider"].ToString() == "Google")
                {
                    try
                    {
                        var url = Request.Url.Query;
                        if (url != "")
                        {
                            string queryString = url.ToString();
                            char[] delimiterChars = { '=' };
                            string[] words = queryString.Split(delimiterChars);
                            string code = words[1];

                            if (code != null)
                            {
                                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                                    webRequest.Method = "POST";
                                    Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_sceret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
                                    byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
                                    webRequest.ContentType = "application/x-www-form-urlencoded";
                                    webRequest.ContentLength = byteArray.Length;
                                    Stream postStream = webRequest.GetRequestStream();
                                    // Add the post data to the web request
                                    postStream.Write(byteArray, 0, byteArray.Length);
                                    postStream.Close();
                                    WebResponse response = webRequest.GetResponse();
                                    postStream = response.GetResponseStream();
                                    StreamReader reader = new StreamReader(postStream);
                                    string responseFromServer = reader.ReadToEnd();
                                    GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);

                                    if (serStatus != null)
                                    {
                                        accessToken = serStatus.access_token;
                                        if (!string.IsNullOrEmpty(accessToken))
                                        {
                                            getgoogleplususerdataSer(accessToken);
                                        }
                                    }
                            }
                        }
                    }
                    catch (WebException wex)
                    {
                        if (wex.Response != null)
                        {
                            using (var errorResponse = (HttpWebResponse)wex.Response)
                            {
                                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                                {
                                    string error = reader.ReadToEnd();
                                    lblMessage.Text = error;
                                }
                            }
                        }
                    }
                }
            }
    }

protected void btnGoogleLogin_Click(object sender, System.EventArgs e)
    {
        var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id + "&state=" + Session["state"];
        Session["Provider"] = "Google";
        Response.Redirect(Googleurl);
    }

我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

我决定避免使用state参数。相反,我将ReturnUrl存储在cookie中,以便在成功验证后使用。

returnPath = Request.QueryString["ReturnUrl"];
        if (returnPath == null)
        {
            returnPath = "ManageAccount.aspx";
        }
        else
        {
            returnPath = Request.QueryString["ReturnUrl"].ToString();
        }
        //Create a cookie to redirect user after login
        HttpCookie rCookie = new HttpCookie("RedirectCookie");
        DateTime now = DateTime.Now;
        rCookie.Value = returnPath;
        Response.Cookies.Add(rCookie);