OnSecurityTokenValidated method in OwinMiddleware is not getting called everytime

40 Views Asked by At

private Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
        {
            Log.Info("Line:121 OwinMiddleWare.OnSecurityTokenValidated", this);
            try
            {
                Log.Info("NameClaimType" + Globals.NameClaimType, this);
                Log.Info("PVGroupsAD" + Globals.PVGroupsAD, this);
                var protectedState = notification.ProtocolMessage.State.Split('=')[1];
                Log.Info("127:protectedState" + protectedState, this);
                var state = notification.Options.StateDataFormat.Unprotect(protectedState);
                Log.Info("state" + state, this);
                //get idtoken in notification.protocol
                var Idtoken = notification.ProtocolMessage.IdToken;
                Log.Info("Idtoken" + Idtoken, this);
                var handler = new JwtSecurityTokenHandler();
                Log.Info("Line:133 handler" + handler, this);
                var jsonToken = handler.ReadToken(Idtoken);
                Log.Info("135:jsonToken" + jsonToken, this);
                var tokenS = jsonToken as JwtSecurityToken;
                Log.Info("Line:136 tokenS" + tokenS, this);
                var tokenExp = tokenS.Claims.First(claim => claim.Type.Equals("exp")).Value;
                Log.Info("Line:138 tokenExp" + tokenExp, this);
                DateTime owin_valid_date;
                DateTime.TryParseExact(tokenExp, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out owin_valid_date);
                Log.Info("Line:141 owin_valid_date" + owin_valid_date, this);
                var ticks = long.Parse(tokenExp);
                Log.Info("Line:143 ticks" + ticks, this);
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                Log.Info("Line:145 origin" + origin, this);
                var expiryTime = origin.AddSeconds(ticks);
                Log.Info("Line:147 expiryTime" + expiryTime, this);
                var now = DateTime.Now.ToUniversalTime();
                Log.Info("Line:149 now" + now, this);
                var valid = expiryTime >= now;
                Log.Info("Line:151 valid" + valid, this);
                Log.Info("Line:152 IDToken"+Idtoken.ToString(),this);
                var jti = tokenS.Claims.First(claim => claim.Type == Globals.NameClaimType).Value;
                Log.Info("Line:154 jti" +jti,this);
                if (!valid)
                {
                    HttpContext.Current.Session["internalUserToken"] = "1";
                    Log.Info("10Owin-internalUserToken" + HttpContext.Current.Session["internalUserToken"], this);
                }
                if (!Globals.NameClaimType.Equals("name") && Globals.PVGroupsAD.Contains(jti))
                {
                    HttpContext.Current.Session["internalUserToken"] = Idtoken.ToString();
                    HttpCookie cookie = new HttpCookie("internalUserTokenCookie", Idtoken.ToString());
                    //cookie.Expires = DateTime.Now.AddHours(1);
                    Log.Info("11Owin-internalUserToken" + HttpContext.Current.Session["internalUserToken"], this);
                }
                else
                if (!Globals.NameClaimType.Equals("name"))
                {
                    HttpContext.Current.Session["internalUserToken"] = "1";
                    Log.Info("12Owin-internalUserToken" + HttpContext.Current.Session["internalUserToken"], this);
                }
                else
                {
                    HttpContext.Current.Session["internalUserToken"] = Idtoken.ToString();
                    Log.Info("13Owin-internalUserToken" + HttpContext.Current.Session["internalUserToken"], this);
                }
                Log.Info("Line:178 OnSecurityTokenValidate.InternalToken"+ HttpContext.Current.Session["internalUserToken"]?.ToString(),this);
            }
            catch (Exception ex)
            {
                Log.Error("Error in OnSecurityTokenValidate", ex.Message);
            }
            return Task.FromResult(0);
        }

We have added the sitecore logs to check whether the method is getting called or not. we are not seeing any logs under OnsecurityTokenValidate method is getting printed. We need to method to get fired everytime when the owin middleware is getting called by sitecore pipeline.

  1. OwinMiddleware is getting called
  2. Getting the logs which we have added inside the Startup Class
  3. OnSecurityCodeValidate Method is not getting called.
0

There are 0 best solutions below