Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get email address from OpenID Provider with DotNetOpenAuth

I can't get the email address returned in GetExtension method, but it is included in the url that Google (the OP I'm testing with) sends back to me.

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

Anyone knows what's wrong?

like image 655
Carol Avatar asked Apr 28 '11 05:04

Carol


2 Answers

Try the following code:

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;
    //...
}
like image 123
Kamyar Avatar answered Sep 27 '22 21:09

Kamyar


None of the above worked for me (using PayPal Access as a identifier) in C#

The below worked for me:

    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();

    }
like image 26
Ian Cullen Avatar answered Sep 27 '22 21:09

Ian Cullen