Example #1
0
        public static string CalculateHash(IConfiguration configurationService, JwtData data)
        {
            if (data == null || configurationService == null)
            {
                return(null);
            }

            var instanteToCalculate = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtData>(
                Newtonsoft.Json.JsonConvert.SerializeObject(data)
                );

            instanteToCalculate.Hash = null;
            return(String.Join("", System.Security.Cryptography.SHA256.Create().ComputeHash(
                                   Encoding.UTF8.GetBytes(
                                       String.Concat(configurationService.GetSection("Authentication:Seed").Value, Newtonsoft.Json.JsonConvert.SerializeObject(data), configurationService.GetSection("Authentication:Sufix").Value)
                                       )
                                   ).Select(x => x.ToString("X2"))).ToLower());
        }
Example #2
0
        public ContextIronMountain(IConfiguration config, ClaimsPrincipal principal)
        {
            if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated && principal.Claims != null)
            {
                foreach (var claim in principal.Claims)
                {
                    switch (claim.Type)
                    {
                    case ClaimTypes.Name:
                        this.Name = claim.Value;
                        break;

                    case ClaimTypes.UserData:
                        var data = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtData>(claim.Value);
                        this.UserData = data;
                        this.IsValid  = data.Hash == JwtData.CalculateHash(this.config, data);
                        break;

                    default:
                        break;
                    }
                }
            }
        }