Retrieving user information from Open Id using Open Authentication Module for Orchard CMS

#orchard #openauthentication

Posted by admin on September 01, 2019

Note : The Open authentication module is a work in progress this may change, though I thought I would blog about it because I thought it was cool. Also all code is on Dev branch at the moment.

So.. picture this…. Your users are authenticating in to your system with all their profile details held across multiple open id providers, and you have just implemented an User Profile module where your users can insert data about themselves. Wouldn't it be nice that when users register for the first time and authenticate every subsequent time thereafter, that your profile module could take the details held by individual providers and pass them on to your profiles module? Well now the open authentication module provides an interface to do this (Open ID only at the moment at it has been kind of a spike)

So here are the step to produce the results once I check the code in

  1. Make sure you have a reference to the NGM.OpenAuthentication module.
  2. Next thing to do is to create a class that implements IClaimsHandler as so.. this class should exist in any module that needs claims/user information from the open id provider.
using System;
using NGM.OpenAuthentication.Core;


namespace NGM.Profiles.Core {
    public class UserProfileClaims : IClaimsHandler {
        public void Creating() {
            Console.WriteLine("Creating");
        }



    <span class="kwrd">public</span> <span class="kwrd">void</span> Created(dynamic claims) {
        Console.WriteLine(<span class="str">"Created"</span>);
        Console.WriteLine(claims.Contact.Email);
    }
}


}
  1. Make sure your module is enabled. (if your not sure on that see this post http://www.orchardproject.net/docs/Enabling-and-disabling-features.ashx)

  2. Go sign in for example thru Google using the open authentication mechanism built in to the Open Authentication module.

  3. On successful authentication two methods will be called.

  4. Before doing anything with claims ‘Creating’ is called,

  5. Once the claims have been built you can then retrieve them thru the implemented Created method.

I have used the dynamic keyword to build up my objects as I have been experimenting…. An example of the code building up the claims is below :-

            dynamic claims = new ExpandoObject();
            claims.BirthDate = new ExpandoObject();
            claims.BirthDate.DayOfMonth = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.DayOfMonth);
            claims.BirthDate.Month = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.Month);
            claims.BirthDate.WholeBirthDate = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.WholeBirthDate);
            claims.BirthDate.Year = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.Year);


        claims.Company = <span class="kwrd">new</span> ExpandoObject();
        claims.Company.CompanyName = fetchResponse.GetAttributeValue(WellKnownAttributes.Company.CompanyName);
        claims.Company.JobTitle = fetchResponse.GetAttributeValue(WellKnownAttributes.Company.JobTitle);

        claims.Contact = <span class="kwrd">new</span> ExpandoObject();
        claims.Contact.Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email);</pre>

I'm not sure whether I will continue with the dynamic key word as it may be difficult for module developers to use. Thoughts?

Anyways… That's how to get Open Id claims out of the Open Authentication module and so I thought I would share it with you…

Feedback would be awesome Smile (I will check this code in tomorrow for people to play with)

Nick