在我的ASP.net网站上通过身份验证从Google帐户中获取用户信息

时间:2012-07-18 03:13:38

标签: c# asp.net

这个关于谷歌身份验证的问题,当我尝试运行下面的代码页成功加载但当我点击登录与谷歌的按钮然后调试显示错误在这一行。

时出错
GetExtension<FetchResponse>() 

在线

Var fetchResponse = Response.GetExtension<FetchResponse>()

那么这一行有什么问题..我试着搜索这个问题,但我没有得到那个解决方案

protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty rp = new OpenIdRelyingParty();
        var r = rp.GetResponse();
        if (r != null)
        {
            switch (r.Status)
            {
                case AuthenticationStatus.Authenticated:
                    NotLoggedIn.Visible = false;
                    Session["GoogleIdentifier"] = r.ClaimedIdentifier.ToString();
                    Response.Redirect("Default4.aspx"); //redirect to main page of your website  
                    var fetchResponse = Response.GetExtension<FetchResponse>();
                    Session["FetchResponse"] = fetchResponse;
                    var response2 = Session["FetchResponse"] as FetchResponse;
                    // Use FormsAuthentication to tell ASP.NET that the user is now logged in,
                    // with the OpenID Claimed Identifier as their username.
                    string uname = response2.GetAttributeValue(WellKnownAttributes.Name.First) ?? "Guest";
                    FormsAuthentication.RedirectFromLoginPage(uname, false);   //(response.ClaimedIdentifier, false);
                    break;

                case AuthenticationStatus.Canceled:
                    lblAlertMsg.Text = "Cancelled.";
                    break;

                case AuthenticationStatus.Failed:
                    lblAlertMsg.Text = "Login Failed.";
                    break;
            }
        }
    }

    protected void OpenLogin_Click(object src, CommandEventArgs e)
    {
        string discoveryUri = e.CommandArgument.ToString();
        OpenIdRelyingParty openid = new OpenIdRelyingParty();
        var b = new UriBuilder(Request.Url) { Query = "" };
        //var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);


        //var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);
        var req = openid.CreateRequest(discoveryUri);

        // This is where you would add any OpenID extensions you wanted
        // to include in the authentication request. In this case, we are making use of OpenID Attribute Exchange 1.0
        // to fetch additional data fields from the OpenID Provider
        var fetchRequest = new FetchRequest();
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
        req.AddExtension(fetchRequest);


        req.RedirectToProvider();

    }

1 个答案:

答案 0 :(得分:1)

应该是

var fetchResponse = r.GetExtension<FetchResponse>();

而不是

var fetchResponse = Response.GetExtension<FetchResponse>();