• About Morris Development
  • A Focus for Cloud Efficiency
    • Microsoft Azure
    • Amazon Web Services
  • What our Clients Say
  • Our Products
    • PreschoolDB
    • WebinarDB
  • Contact Us

Morris Development

Custom System Development and Integration

April 14, 2020

Auth0 Authentication and Validation

Open Authentication is pretty standard by now, and for us, a requirement. We don’t maintain our clients’ passwords, allowing their administration to handle that. Instead, we use oAuth to access sites. While getting the token from a Javascript page is pretty straight forward, what we really need is a way to then access our own API servers with that token and ensure this user is legit. To do so, we have an api that receives a posted token object like this {token:”blah blah blah”}, we then call the Auth0 website where they keep our matching credentials and see if the token is ok. Then, we give them a session ID on our server and in our database.

Now, this is stripped down from our normal session controller, but basically, this is called after the promise call to Auth0 has been fulfilled. We simply relay that call right back to our server, get the reply and then our user is officially “logged in”.

public class SessionController : ApiController
    {
        const string auth0Domain = "https://[your account].auth0.com/";

        /* 
           This is the clientID of your Auth0 application, 
           not the url of the "Audience", like most examples
           I found on the web 
       */
        const string auth0ClientID = "hYbChvXUxxxxxxxxxxxxxxxxxxRuOSWHvH";


        [ResponseType(typeof(JObject ))]
        public async Task<IHttpActionResult> Post([FromBody]JObject obj, [FromUri]string sid = "")
        {


            string token = (string)obj["token"];

            var openidConfiguration = await OpenIdConnectConfigurationRetriever.GetAsync(
                 $"{auth0Domain}.well-known/openid-configuration", CancellationToken.None).ConfigureAwait(false);



            TokenValidationParameters validationParameters =
                new TokenValidationParameters
                {
                    ValidIssuer = auth0Domain,   
                    ValidAudiences = new[] { auth0ClientID },  
                    IssuerSigningKeys = openidConfiguration.SigningKeys
                };

            SecurityToken validatedToken;
            JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
            var user = handler.ValidateToken(token, validationParameters, out validatedToken);

            if (user.Identity.IsAuthenticated)
            {

     /*   
     in here, I send the token off to get some
     further information from our database, cache the token, etc..
     You can do whatever you need to do.
     */
                SecurityModule sec = new SecurityModule();                
                dynamic responseObject = sec.getSessionId(obj );
                return Ok(responseObject);

            }
            else
            {
                return Unauthorized();
            }
        }
}

Article by MacGyver / Angular 2 / Auth0, Authentication, C#, oAuth

About MacGyver

I've worked with database systems for over 20 years, and started my own company in 2000. Almost all my business consists of internal database systems, either ERP or CRM. My programming is primarily in Angular / Microsoft C# and MS SQL.

About This Site

Morris Development has been specializing in internal database system design and integration since 1999. We provide long-term management and support of secure data systems for many businesses as well as developing the more complex code structures for ERP systems like Intellievent, Apidas, and AVMS.

This site is primarily for our developers to keep track up various technologies and updates that are used by Morris Development.

Training

Integrating Angular Microsite with .Net

Private Data Caching with Google Storage

Continuous Deployment for Production Releases?

Azure Websites – the perfect Angular host

Angular 2

  • Angular 2 Authentication
  • Angular Command Line Interface
  • Material Design for Angular
  • Using Observables in Angular 2

Mentors

  • Ben Nadel
  • Dan Wahlin
  • Deborah Kurata
  • John Papa

Staff

  • Dan Morris

Training

  • Google Development Courses
  • Microsoft Virtual Academy
  • PluralSight
  • Test Deep Links

© 2025 · Morris Development