views:

757

answers:

3

What's the secret to getting ClaimsResponse working with DotNetOpenId?

For example, in this bit of code (from Scott Hanselman's blog) the ClaimsResponse object should have lots of nice little things like 'nickname' and 'email address', but the ClaimsResponse object itself is 'null':

 OpenIdRelyingParty openid = new OpenIdRelyingParty();  
 if (openid.Response != null)  
 {  
    // Stage 3: OpenID Provider sending assertion response  
    switch (openid.Response.Status)  
    {  
       case AuthenticationStatus.Authenticated:  
          ClaimsResponse fetch = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;  
          string nick = fetch.Nickname;  
          string homepage = openid.Response.ClaimedIdentifier;  
          string email = fetch.Email;  
          string comment = Session["pendingComment"] as string;  
          string entryId = Session["pendingEntryId"] as string;  
          if (String.IsNullOrEmpty(comment) == false && String.IsNullOrEmpty(entryId) == false)  
          {  
             AddNewComment(nick, email, homepage, comment, entryId, true);  
          }  
          break;  
    }  
 }

At first, I thought it was because I wasn't redirecting to the provider with a 'ClaimsRequest' ... but using this code to redirect to the OpenId provider still doesn't help:

OpenIdRelyingParty openid = new OpenIdRelyingParty();  
IAuthenticationRequest req = openid.CreateRequest(openid_identifier.Text);  
ClaimsRequest fetch = new ClaimsRequest();  
fetch.Email = DemandLevel.Require;  
fetch.Nickname = DemandLevel.Require;  
req.AddExtension(fetch);  
req.RedirectToProvider();

What am I doing wrong? Or have other devs experienced the same pain?

+2  A: 

With the latests version of DotNetOpenId, this code seems to work fine for me:

var request = openid.CreateRequest(openid_identifier);
var fields = new ClaimsRequest();
fields.Email = DemandLevel.Require;
fields.Nickname = DemandLevel.Require;
request.AddExtension(fields);
request.RedirectToProvider();

on return from provider:

var claimResponse = openid.Response.GetExtension<ClaimsResponse>();

PS: I'm using MVC, not WebForms.

OJ
I'm really curious -- what OpenId provider are you using?
Dan Esparza
same as you mate, DotNetOpenId.
OJ
No no -- I mean when you connect to validate your OpenId; are you using Yahoo, AOL, Wordpress, MyOpenId, etc?
Dan Esparza
I am able to validate against MyOpenId and Verisign labs in my testing. It should work fine with any openID provider.
OJ
+2  A: 

Your code looks fine. But be aware that the sreg extension, which you are using, isn't supported by all OPs. If the OP you're authenticating with doesn't support it, then the response extension will be null as you're seeing. So a null check is always a good idea.

myopenid.com supports sreg, if you're looking for an OP to test against.

Andrew Arnott
+1  A: 

I really don't know how OJ got ClaimsResponse. I test with Google and MyOpenID. But I still got null value of ClaimsResponse object. OJ, can you please help us?

You'll never get ClaimsResponse from Google because Google doesn't support sreg. MyOpenID does, so assuming you used code similar to the above examples, you should get a non-null response as long as the authenticating user has data in his profile and approves sending that data.
Andrew Arnott