OpenID:尝试从Google OP获取电子邮件地址

Zaf*_*iro 35 c# openid dotnetopenauth

我正在使用dotnetopenauth 3.2来实现Openid,并且无法弄清楚如何让Google在声明响应中传递电子邮件地址.我知道Google不支持简单注册,但我无法确定他们支持的内容.

对这个问题的警告是我刚刚开始学习OpenID,我知道我对规范没有扎实的把握,我认为这导致了我的困惑.

任何帮助,将不胜感激!

Zaf*_*iro 52

好想通了.我在Goolge的Federated Log API组上发布了一个问题,并被告知要使用属性交换.

以下是DotNetOpenAuth的代码.

请不要在生产中使用此代码.这仅用于说明目的!

请求:

using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
{
    IAuthenticationRequest request = openid.CreateRequest(openidurl);

    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    request.AddExtension(fetch);

    // Send your visitor to their Provider for authentication.
    request.RedirectToProvider();
}
Run Code Online (Sandbox Code Playgroud)

响应:

OpenIdRelyingParty openid = new OpenIdRelyingParty();
var response = openid.GetResponse();
if (response != null)
{
    switch (response.Status)
    {
        case AuthenticationStatus.Authenticated:
        {
            var fetch = response.GetExtension<FetchResponse>();
            string email = string.Empty();
            if (fetch != null)
            {
                email =  fetch.GetAttributeValue(
                    WellKnownAttributes.Contact.Email);
            }

            FormsAuthentication.RedirectFromLoginPage(
                response.ClaimedIdentifier, false);
            break;
        }
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在上面的代码的帮助下,您还可以获得用户的名字和姓氏 - 谢谢zaffiro (2认同)