加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:20:14 所属栏目:百科 来源:网络整理
导读:我无法获取GetExtension方法中返回的电子邮件地址,但它包含在Google(我正在测试的OP)发回给我的网址中. if (Page.IsPostBack){ using (var openid = new OpenIdRelyingParty()) { var request = openid.CreateRequest(Request.Form["openid_identifier"]); v
我无法获取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;
    //...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读