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

前端之家收集整理的这篇文章主要介绍了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;
    //...
}

猜你在找的C#相关文章