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

时间:2011-04-28 05:43:35

标签: c# .net dotnetopenauth

我无法获取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;
            }
        }
    }
}

任何人都知道什么是错的?

2 个答案:

答案 0 :(得分:6)

请尝试以下代码:

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;
    //...
}

答案 1 :(得分:2)

以上都不适用于我(使用PayPal Access作为标识符)在C#

以下对我有用:

    OpenIdRelyingParty openid = new OpenIdRelyingParty();

    protected void Page_Load(object sender, EventArgs e)
    {
        var response = openid.GetResponse();

        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:

                    if (this.Request.Params["openid.ext1.value.alias1"] != null)
                    {
                        Response.Write(this.Request.Params["openid.ext1.value.alias1"]);
                        Response.Write(this.Request.Params["openid.ext1.value.alias2"]);
                    }
                    else {
                        Response.Write("Alias wrong");
                    }
                    break;
            }
        }
    }
     protected void loginButton_Click(object sender, EventArgs e)
    {

        var openidRequest = openid.CreateRequest(openIdBox.Text);
        var fetch = new FetchRequest();

        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
        openidRequest.AddExtension(fetch);

        openidRequest.RedirectToProvider();

    }