我正在使用dotnetopenauth 3.2来实现Openid,并且无法弄清楚如何让Google在声明响应中传递电子邮件地址。我知道Google不支持简单注册,但我无法确定他们支持的内容。
对这个问题的警告是我刚刚开始学习OpenID,我知道我对规范没有扎实的把握,我认为这导致了我的困惑。
任何帮助将不胜感激!
答案 0 :(得分:52)
好的想通了。我在Goolge's Federated Log API group上发布了一个问题,并被告知要使用Attribute exchange。
以下是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();
}
回应:
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;
}
...
}
}
答案 1 :(得分:1)
当我尝试获取全名时响应为空,请提供获取全名的解决方案, 这篇文章真的很有帮助 谢谢。 我的示例代码就像这样。
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
fetch.Attributes.AddRequired(WellKnownAttributes.Company.CompanyName);
//fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
request.AddExtension(fetch);
和强>
if (fetch != null)
{
email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
name = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
company = fetch.GetAttributeValue(WellKnownAttributes.Company.CompanyName);
}