5

我正在尝试使用 dotNetOpenId 登录到 GMail 帐户。它有效,但我无法检索任何索赔。我知道我也可以检索电子邮件地址或用户名,但是只有ClaimedIdentifier可用,才会返回任何声明。有人知道如何从 Gmail 帐户中检索这些数据吗?如果您能提供一个 ClaimsRequest 配置的示例,我将不胜感激。

谢谢

4

1 回答 1

2
// Either you're creating this already or you can get to it in 
// the LoggingIn event of the control you're using.

IAuthenticationRequest request;

// Add the AX request that says Email address is required.
var fetch = new FetchRequest();
fetch.Attributes.Add(
    new AttributeRequest(WellKnownAttributes.Contact.Email, true));
request.AddExtension(fetch);

然后,Google 对用户进行身份验证并返回您可以使用的电子邮件地址:

var fetch = openid.Response.GetExtension<FetchResponse>();  
if (fetch != null) 
{  
    IList<string> emailAddresses = fetch.GetAttribute(
        WellKnownAttributes.Contact.Email).Values;  
    string email = emailAddresses.Count > 0 ? emailAddresses[0] : null;  
}

您可以查看我关于该主题的博客文章以获取更多信息。这里需要注意的重要一点是,如果您将其标记为必需,Google 只会告诉您用户的电子邮件地址(正如我在上面的代码段中所做的那样)。但这也意味着如果用户不想分享他的电子邮件地址,他就根本无法登录。抱歉,Google 就是这样设置的。不幸的是,人们使用的其他提供者有不同的行为。

于 2009-04-10T13:43:51.930 回答