Esempio n. 1
0
 public IHttpActionResult GetAllExpired()
 {
     try
     {
         AdalClient _client         = new AdalClient(_appConfig, CredentialType.Client, null);
         ServicePrincipalFactory _f = new ServicePrincipalFactory();
         var _manager           = _f.CreateInstance(_client);
         var _servicePrincipals = _manager.GetExpiredPrincipals();
         return(Ok(_servicePrincipals));
     }
     catch (TIPException ex)
     {
         var _response = new ErrorResponse();
         _response.Error = ex.Error;
         return(Content(HttpStatusCode.InternalServerError, _response));
     }
     catch (Exception ex)
     {
         var _response = new ErrorResponse();
         _response.Error = new Error
         {
             Code    = Common.Constants.ErrorCodes.GENERAL,
             Message = ex.Message
         };
         return(Content(HttpStatusCode.InternalServerError, _response));
     }
 }
Esempio n. 2
0
        public static void ProcessAADObjects([TimerTrigger("0 0 1 * * 1-5")] TimerInfo info, TextWriter log)          // 01:00 every weekday
        {
            try
            {
                AdalClient client = new AdalClient(appConfig, CredentialType.Client, null);

                var appFactory       = new ApplicationFactory();
                var appManager       = appFactory.CreateInstance(client);
                var appsExpiringSoon = appManager.GetExpiredApplicationInDays(appConfig.NotificationInterval);
                var appsExpired      = appManager.GetAllExpired();

                var spFactory      = new ServicePrincipalFactory();
                var spManager      = spFactory.CreateInstance(client);
                var spExpiringSoon = spManager.GetExpiredPrincipalsInDays(appConfig.NotificationInterval);
                var spExpired      = spManager.GetExpiredPrincipals();

                if (appsExpiringSoon.Count > 0 || appsExpired.Count > 0 ||
                    spExpiringSoon.Count > 0 || spExpired.Count > 0)
                {
                    // Create the Connector Card payload
                    var card = new ConnectorCard
                    {
                        Summary    = "Expiring Credential Status",
                        Title      = "PnP Tools - Tenant Information Portal",
                        Text       = "The credentials for the following have expired or will expire soon.",
                        ThemeColor = "#FF0000"
                    };
                    card.PotentialAction.Add(
                        new ViewAction
                    {
                        Name   = "View in Tenant Information Portal",
                        Target = new string[] { appConfig.PortalUrl }
                    }
                        );


                    List <Fact> facts = null;
                    if (appsExpiringSoon.Count > 0)
                    {
                        facts = appsExpiringSoon.Select(a => new Fact {
                            Name = a.DiplayName, Value = a.EndDate.ToString()
                        }).ToList();
                        card.Sections.Add(CreateSection($"Applications Expiring Soon ({appConfig.NotificationInterval}) days", ExpiringSoonImage, facts));
                    }

                    if (appsExpired.Count > 0)
                    {
                        facts = appsExpired.Select(a => new Fact {
                            Name = a.DiplayName, Value = a.EndDate.ToString()
                        }).ToList();
                        card.Sections.Add(CreateSection("Applications Expired", ExpiredImage, facts));
                    }

                    if (spExpiringSoon.Count > 0)
                    {
                        facts = spExpiringSoon.Select(sp => new Fact {
                            Name = sp.DisplayName, Value = sp.EndDate.ToString()
                        }).ToList();
                        card.Sections.Add(CreateSection($"Service Principals Expiring Soon ({appConfig.NotificationInterval}) days", ExpiringSoonImage, facts));
                    }

                    if (spExpired.Count > 0)
                    {
                        facts = spExpired.Select(sp => new Fact {
                            Name = sp.DisplayName, Value = sp.EndDate.ToString()
                        }).ToList();
                        card.Sections.Add(CreateSection("Service Principals Expired", ExpiredImage, facts));
                    }

                    var requestBody = JsonConvert.SerializeObject(card, null, new JsonSerializerSettings {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    });

                    // Make POST to webhook URL
                    var status = HttpHelper.PostJsonMessage(appConfig.ConnectorUrl, requestBody);
                }
            }
            catch (Exception ex)
            {
                log.Write(ex.ToString());
            }
        }