c# – 使用DotNetOpenAuth从OpenID Provider获取电子邮件地址

我无法获取GetExtension方法中返回的电子邮件地址,但它包含在Google(我正在测试的OP)发回给我的网址中.
if (Page.IsPostBack)
{
    using (var openid = new OpenIdRelyingParty())
    {
        var request = openid.CreateRequest(Request.Form["openid_identifier"]);

        var fetch = new FetchRequest();
        fetch.Attributes.Add(new AttributeRequest(WellKNownAttributes.Contact.Email,true));

        request.AddExtension(fetch);

        request.RedirectToProvider();
    }
}
else
{
    using (var openid = new OpenIdRelyingParty())
    {
        var response = openid.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    var claimsResponse = response.GetExtension<FetchRequest>();
                    break;
                case AuthenticationStatus.Canceled:
                    //this.loginCanceledLabel.Visible = true;
                    break;
                case AuthenticationStatus.Setuprequired:
                    //this.loginFailedLabel.Visible = true;
                    break;

                // We don't need to handle Setuprequired because we're not setting
                // IAuthenticationRequest.Mode to immediate mode.
                ////case AuthenticationStatus.Setuprequired:
                ////    break;
            }
        }
    }
}

谁知道什么是错的?

解决方法

请尝试以下代码
switch (response.Status)
 {
     case AuthenticationStatus.Authenticated:
         var fetch = response.GetExtension<FetchResponse>();
         string email = String.Empty; 
         if (fetch != null)
         {
            email =  fetch.GetAttributeValue(WellKNownAttributes.Contact.Email);
         }  
        break;
    //...
}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...