views:

188

answers:

1

I am using DotNetOpenAuth to integrate openID in our web application. The code below requests the information to the provider.

try
{
  var req = openid.CreateRequest(Request.Form["openid_identifier"]);
  req.AddExtension(new DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.ClaimsRequest
  {
    Email = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Require,
    FullName = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Require,
    Nickname = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Request,
    PostalCode = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Request
  });

  return req.RedirectingResponse.AsActionResult();
}

For some reason the response from the openID provider never comes with the information I am requesting. Below is the code:

// Stage 3: OpenID Provider sending assertion response
switch (response.Status) {
  case AuthenticationStatus.Authenticated:
    Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
    FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
    if (!string.IsNullOrEmpty(returnUrl)) {
       return Redirect(returnUrl);
    } else {
       return RedirectToAction("Index", "Home");
    }

I have tried: response.ClaimedIdentifier in a million ways and it never has valuable information that I can do something with. Any ideas?

+5  A: 

The IAuthenticationResponse.ClaimedIdentifier property never contains these attributes that you're requesting. It only contains the "username" of the OpenID user.

You're sending the request perfectly. Just add a bit to your handling of the positive response:

// Stage 3: OpenID Provider sending assertion response
switch (response.Status) { 
  case AuthenticationStatus.Authenticated:
    Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay; 
    FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
    var sreg = response.GetExtension<ClaimsResponse>();
    if (sreg != null) { // the Provider MAY not provide anything
      // and even if it does, any of these attributes MAY be missing
      var email = sreg.Email;
      var fullName = sreg.FullName;
      // get the rest of the attributes, and store them off somewhere.
    }
    if (!string.IsNullOrEmpty(returnUrl)) {
      return Redirect(returnUrl);
    } else {
       return RedirectToAction("Index", "Home");
    }
  break;
  // ...
Andrew Arnott
Hi Andrew: Thanks very much it works. Google gives back the email, Yahoo does not. I appreciate your help.
Geo
Yahoo should be now... they just upgraded to support it.
Andrew Arnott