GetEncryptedPassword() public static méthode

This method will return the secure password for authentication to SharePoint Online
public static GetEncryptedPassword ( string plainTextPassword ) : SecureString
plainTextPassword string
Résultat System.Security.SecureString
Exemple #1
0
        private static void SentEmailNotificationForCreatedIsBackwardCompatibleMatters(MatterInformationVM matterInformation1, ExchangeService service, TextWriter log, IConfigurationRoot configuration)
        {
            string mailSubject                     = configuration.GetSection("Mail").GetSection("MatterMailSubject").Value;
            string defaultHtmlChunk                = configuration.GetSection("Mail").GetSection("MatterMailDefaultContentTypeHtmlChunk").Value;
            string oneNoteLibrarySuffix            = configuration.GetSection("Matter").GetSection("OneNoteLibrarySuffix").Value;
            string matterMailBodyMatterInformation = configuration.GetSection("Mail").GetSection("MatterMailBodyMatterInformation").Value;
            string matterMailBodyConflictCheck     = configuration.GetSection("Mail").GetSection("MatterMailBodyConflictCheck").Value;
            string matterCenterDateFormat          = configuration.GetSection("Mail").GetSection("MatterCenterDateFormat").Value;
            string matterMailBodyTeamMembers       = configuration.GetSection("Mail").GetSection("MatterMailBodyTeamMembers").Value;
            ///For isbackward compatability true
            MatterInformationVM originalMatter = JsonConvert.DeserializeObject <MatterInformationVM>(matterInformation1.SerializeMatter);
            Matter        matter                  = originalMatter.Matter;
            MatterDetails matterDetails           = originalMatter.MatterDetails;
            Client        client                  = originalMatter.Client;
            string        practiceGroupColumnName = configuration["ContentTypes:ManagedColumns:ColumnName1"].ToString();
            string        areaOfLawColumnName     = configuration["ContentTypes:ManagedColumns:ColumnName2"].ToString();
            string        matterMailBody;
            string        practiceGroupValue = originalMatter.MatterDetails.ManagedColumnTerms[practiceGroupColumnName].TermName;
            string        areaOfLawValue     = originalMatter.MatterDetails.ManagedColumnTerms[areaOfLawColumnName].TermName;

            // Generate Mail Subject
            string matterMailSubject = string.Format(CultureInfo.InvariantCulture, mailSubject,
                                                     matter.Id, matter.Name, originalMatter.MatterCreator);

            // Step 1: Create Matter Information
            string defaultContentType = string.Format(CultureInfo.InvariantCulture,
                                                      defaultHtmlChunk, matter.DefaultContentType);
            string matterType = string.Join(";", matter.ContentTypes.ToArray()).TrimEnd(';').Replace(matter.DefaultContentType, defaultContentType);

            if (matterType == string.Empty)
            {
                matterType = defaultContentType;
            }
            // Step 2: Create Team Information
            string secureMatter = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.SecureMatter.ToUpperInvariant() ?
                                  ServiceConstants.NO : ServiceConstants.YES;
            string mailBodyTeamInformation = string.Empty;

            mailBodyTeamInformation = TeamMembersPermissionInformation(matterDetails, mailBodyTeamInformation);

            string oneNotePath = string.Concat(client.Url, ServiceConstants.FORWARD_SLASH,
                                               matter.MatterGuid, oneNoteLibrarySuffix,
                                               ServiceConstants.FORWARD_SLASH, matter.Name, ServiceConstants.FORWARD_SLASH, matter.MatterGuid);
            string conflictIdentified = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.Identified.ToUpperInvariant() ?
                                        ServiceConstants.NO : ServiceConstants.YES;

            matterMailBody = string.Format(CultureInfo.InvariantCulture,
                                           matterMailBodyMatterInformation,
                                           practiceGroupValue,
                                           areaOfLawValue,
                                           matter.Name,
                                           matter.Description,
                                           matter.Name + " OneNote",
                                           matterType,
                                           originalMatter.MatterCreator,
                                           string.Format("{0:MMM dd, yyyy}", DateTime.Now),
                                           originalMatter.MatterLocation,
                                           client.Url, oneNotePath,
                                           configuration["General:SiteURL"].ToString());

            EmailMessage email = new EmailMessage(service);

            foreach (IList <string> userNames in matter.AssignUserEmails)
            {
                foreach (string userName in userNames)
                {
                    if (!string.IsNullOrWhiteSpace(userName))
                    {
                        using (var ctx = new ClientContext(originalMatter.Client.Url))
                        {
                            SecureString password = Utility.GetEncryptedPassword(configuration["General:AdminPassword"]);
                            ctx.Credentials = new SharePointOnlineCredentials(configuration["General:AdminUserName"], password);
                            if (CheckUserPresentInMatterCenter(ctx, originalMatter.Client.Url, userName, null, log))
                            {
                                email.ToRecipients.Add(userName);
                            }
                        }
                    }
                }
            }
            string adminUserName = configuration["General:AdminUserName"];

            email.From    = new EmailAddress(adminUserName);
            email.Subject = matterMailSubject;
            email.Body    = matterMailBody;
            email.Send();
            log.WriteLine($"connection string. {configuration["General:CloudStorageConnectionString"]}");
            Utility.UpdateTableStorageEntity(originalMatter, log, configuration["General:CloudStorageConnectionString"],
                                             configuration["Settings:MatterRequests"], "Accepted");
        }
Exemple #2
0
        private static void SentEmailNotificationForCreatedMatters(MatterInformationVM matterInformation1, ExchangeService service, TextWriter log, IConfigurationRoot configuration)
        {
            string mailSubject                     = configuration.GetSection("Mail").GetSection("MatterMailSubject").Value;
            string defaultHtmlChunk                = configuration.GetSection("Mail").GetSection("MatterMailDefaultContentTypeHtmlChunk").Value;
            string oneNoteLibrarySuffix            = configuration.GetSection("Matter").GetSection("OneNoteLibrarySuffix").Value;
            string matterMailBodyMatterInformation = configuration.GetSection("Mail").GetSection("MatterMailBodyMatterInformation").Value;
            string matterMailBodyConflictCheck     = configuration.GetSection("Mail").GetSection("MatterMailBodyConflictCheck").Value;
            string matterCenterDateFormat          = configuration.GetSection("Mail").GetSection("MatterCenterDateFormat").Value;
            string matterMailBodyTeamMembers       = configuration.GetSection("Mail").GetSection("MatterMailBodyTeamMembers").Value;
            //De Serialize the matter information
            MatterInformationVM originalMatter = JsonConvert.DeserializeObject <MatterInformationVM>(matterInformation1.SerializeMatter);
            Matter        matter        = originalMatter.Matter;
            MatterDetails matterDetails = originalMatter.MatterDetails;
            Client        client        = originalMatter.Client;

            string matterMailBody, blockUserNames;
            // Generate Mail Subject
            string matterMailSubject = string.Format(CultureInfo.InvariantCulture, mailSubject,
                                                     matter.Id, matter.Name, originalMatter.MatterCreator);

            // Step 1: Create Matter Information
            string defaultContentType = string.Format(CultureInfo.InvariantCulture,
                                                      defaultHtmlChunk, matter.DefaultContentType);
            string matterType = string.Join(";", matter.ContentTypes.ToArray()).TrimEnd(';').Replace(matter.DefaultContentType, defaultContentType);

            if (matterType == string.Empty)
            {
                matterType = defaultContentType;
            }
            // Step 2: Create Team Information
            string secureMatter = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.SecureMatter.ToUpperInvariant() ?
                                  ServiceConstants.NO : ServiceConstants.YES;
            string mailBodyTeamInformation = string.Empty;

            mailBodyTeamInformation = TeamMembersPermissionInformation(matterDetails, mailBodyTeamInformation);

            string oneNotePath = string.Concat(client.Url, ServiceConstants.FORWARD_SLASH,
                                               matter.MatterGuid, oneNoteLibrarySuffix,
                                               ServiceConstants.FORWARD_SLASH, matter.MatterGuid, ServiceConstants.FORWARD_SLASH, matter.MatterGuid);

            if (originalMatter.IsConflictCheck)
            {
                string conflictIdentified = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.Identified.ToUpperInvariant() ?
                                            ServiceConstants.NO : ServiceConstants.YES;
                blockUserNames = string.Join(";", matter.BlockUserNames.ToArray()).Trim().TrimEnd(';');

                blockUserNames = !String.IsNullOrEmpty(blockUserNames) ? string.Format(CultureInfo.InvariantCulture,
                                                                                       "<div>{0}: {1}</div>", "Conflicted User", blockUserNames) : string.Empty;
                matterMailBody = string.Format(CultureInfo.InvariantCulture,
                                               matterMailBodyMatterInformation, client.Name, client.Id,
                                               matter.Name, matter.Id, matter.Description, matterType) + string.Format(CultureInfo.InvariantCulture,
                                                                                                                       matterMailBodyConflictCheck, ServiceConstants.YES, matter.Conflict.CheckBy,
                                                                                                                       Convert.ToDateTime(matter.Conflict.CheckOn, CultureInfo.InvariantCulture).ToString(matterCenterDateFormat, CultureInfo.InvariantCulture),
                                                                                                                       conflictIdentified) + string.Format(CultureInfo.InvariantCulture,
                                                                                                                                                           matterMailBodyTeamMembers, secureMatter, mailBodyTeamInformation,
                                                                                                                                                           blockUserNames, client.Url, oneNotePath, matter.Name, originalMatter.MatterLocation, matter.Name);
            }
            else
            {
                blockUserNames = string.Empty;
                matterMailBody = string.Format(CultureInfo.InvariantCulture, matterMailBodyMatterInformation,
                                               client.Name, client.Id, matter.Name, matter.Id,
                                               matter.Description, matterType) + string.Format(CultureInfo.InvariantCulture, matterMailBodyTeamMembers, secureMatter,
                                                                                               mailBodyTeamInformation, blockUserNames, client.Url, oneNotePath, matter.Name, originalMatter.MatterLocation, matter.Name);
            }

            EmailMessage email = new EmailMessage(service);

            foreach (IList <string> userNames in matter.AssignUserEmails)
            {
                foreach (string userName in userNames)
                {
                    if (!string.IsNullOrWhiteSpace(userName))
                    {
                        using (var ctx = new ClientContext(originalMatter.Client.Url))
                        {
                            SecureString password = Utility.GetEncryptedPassword(configuration["General:AdminPassword"]);
                            ctx.Credentials = new SharePointOnlineCredentials(configuration["General:AdminUserName"], password);
                            if (CheckUserPresentInMatterCenter(ctx, originalMatter.Client.Url, userName, null, log))
                            {
                                email.ToRecipients.Add(userName);
                            }
                        }
                    }
                }
            }
            email.From    = new EmailAddress(configuration["General:AdminUserName"]);
            email.Subject = matterMailSubject;
            email.Body    = matterMailBody;
            email.Send();
            Utility.UpdateTableStorageEntity(originalMatter, log, configuration["General:CloudStorageConnectionString"],
                                             configuration["Settings:MatterRequests"], "Accepted");
        }
Exemple #3
0
        /// <summary>
        /// This method will get all list items from external access requests and process all
        /// requests which are in accpeted state
        /// </summary>
        /// <param name="originalMatter"></param>
        /// <param name="log"></param>
        /// <param name="configuration"></param>
        private static void GetExternalAccessRequestsFromSPO(MatterInformationVM originalMatter,
                                                             TextWriter log,
                                                             IConfigurationRoot configuration)
        {
            try
            {
                foreach (var assignUserEmails in originalMatter.Matter.AssignUserEmails)
                {
                    foreach (string email in assignUserEmails)
                    {
                        using (var ctx = new ClientContext(originalMatter.Client.Url))
                        {
                            SecureString password = Utility.GetEncryptedPassword(configuration["General:AdminPassword"]);
                            ctx.Credentials = new SharePointOnlineCredentials(configuration["General:AdminUserName"], password);
                            //First check whether the user exists in SharePoint or not
                            log.WriteLine($"Checking whether the user {email} has been present in the system or not");
                            if (CheckUserPresentInMatterCenter(ctx, originalMatter.Client.Url, email, configuration, log) == true)
                            {
                                string    requestedForPerson = email;
                                string    matterId           = originalMatter.Matter.MatterGuid;
                                var       listTitle          = configuration["Settings:ExternalAccessRequests"];
                                var       list      = ctx.Web.Lists.GetByTitle(listTitle);
                                CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();
                                camlQuery.ViewXml = "";
                                ListItemCollection listItemCollection = list.GetItems(camlQuery);
                                ctx.Load(listItemCollection);
                                ctx.ExecuteQuery();
                                log.WriteLine($"Looping all the records from {configuration["Settings:ExternalAccessRequests"]} lists");
                                foreach (ListItem listItem in listItemCollection)
                                {
                                    //The matter id for whom the request has been sent
                                    string requestedObjectTitle = listItem["RequestedObjectTitle"].ToString();
                                    //The person to whom the request has been sent
                                    string requestedFor = listItem["RequestedFor"].ToString();
                                    //The matter url for which the request has been sent
                                    string url = ((FieldUrlValue)listItem["RequestedObjectUrl"]).Url;
                                    //The status of the request whether it has been in pending=0, accepeted=2 or withdrawn=5
                                    string status = listItem["Status"].ToString();
                                    //If the status is accepted and the person and matter in table storage equals to item in Access Requests list
                                    if (requestedFor == requestedForPerson && matterId == requestedObjectTitle && status == "2")
                                    {
                                        log.WriteLine($"The user {email} has been present in the system and he has accepted the invitation and providing permssions to  matter {originalMatter.Matter.Name} from the user {email}");
                                        UpdateMatter umd = new UpdateMatter();
                                        //Update all matter related lists and libraries permissions for external users
                                        umd.UpdateUserPermissionsForMatter(originalMatter, configuration, password);

                                        //Update permissions for external users in Catalog Site Collection
                                        using (var catalogContext = new ClientContext(configuration["General:CentralRepositoryUrl"]))
                                        {
                                            catalogContext.Credentials =
                                                new SharePointOnlineCredentials(configuration["General:AdminUserName"], password);
                                            umd.AssignPermissionToCatalogLists(configuration["Catalog:SiteAssets"], catalogContext,
                                                                               email.Trim(), configuration["Catalog:SiteAssetsPermissions"], configuration);
                                        }
                                        log.WriteLine($"The matter permissions has been updated for the user {email}");
                                        log.WriteLine($"Updating the matter status to Accepted in Azure Table Storage");
                                        Utility.UpdateTableStorageEntity(originalMatter, log, configuration["General:CloudStorageConnectionString"],
                                                                         configuration["Settings:TableStorageForExternalRequests"], "Accepted");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteLine($"Exception occured in the method GetExternalAccessRequestsFromSPO. {ex}");
            }
        }
        /// <summary>
        /// This web job function will process matter that is there in azure table storage called MatterRequests.
        /// If there are any new matter is created, this web job function will get invoked for the time duration that has
        /// been specified and will preocess all new matters and will send notification for those respective users
        /// Once the matter has been processed, it will change the status of that matter to "Send" so that it will not
        /// be processed again
        /// </summary>
        /// <param name="timerInfo"></param>
        /// <param name="matterInformationVM"></param>
        public static void ProcessMatter([TimerTrigger("00:00:05", RunOnStartup = true)] TimerInfo timerInfo,
                                         [Table("MatterRequests")] IQueryable <MatterInformationVM> matterInformationVM, TextWriter log)
        {
            var query = from p in matterInformationVM select p;

            if (query.ToList().Count() > 0)
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appSettings.json")
                              .AddInMemoryCollection()
                              .AddEnvironmentVariables();//appsettings.json will be overridden with azure web appsettings
                ExchangeService service       = new ExchangeService(ExchangeVersion.Exchange2013);
                var             configuration = builder.Build();
                KeyVaultHelper  kv            = new KeyVaultHelper(configuration);
                KeyVaultHelper.GetCert(configuration);
                kv.GetKeyVaultSecretsCerticate();
                //// can use on premise exchange server credentials with service.UseDefaultCredentials = true, or
                //explicitly specify the admin account (set default to false)
                string adminUserName = configuration.GetSection("General").GetSection("AdminUserName").Value;
                string adminPassword = configuration.GetSection("General").GetSection("AdminPassword").Value;
                service.Credentials = new WebCredentials(adminUserName, adminPassword);
                service.Url         = new Uri(configuration.GetSection("Settings").GetSection("ExchangeServiceURL").Value);
                string mailSubject                     = configuration.GetSection("Mail").GetSection("MatterMailSubject").Value;
                string defaultHtmlChunk                = configuration.GetSection("Mail").GetSection("MatterMailDefaultContentTypeHtmlChunk").Value;
                string oneNoteLibrarySuffix            = configuration.GetSection("Matter").GetSection("OneNoteLibrarySuffix").Value;
                string matterMailBodyMatterInformation = configuration.GetSection("Mail").GetSection("MatterMailBodyMatterInformation").Value;
                string matterMailBodyConflictCheck     = configuration.GetSection("Mail").GetSection("MatterMailBodyConflictCheck").Value;
                string matterCenterDateFormat          = configuration.GetSection("Mail").GetSection("MatterCenterDateFormat").Value;
                string matterMailBodyTeamMembers       = configuration.GetSection("Mail").GetSection("MatterMailBodyTeamMembers").Value;

                foreach (MatterInformationVM matterInformation1 in query)
                {
                    if (matterInformation1 != null)
                    {
                        if (matterInformation1.Status.ToLower() == "pending")
                        {
                            //De Serialize the matter information
                            MatterInformationVM originalMatter = JsonConvert.DeserializeObject <MatterInformationVM>(matterInformation1.SerializeMatter);
                            Matter        matter        = originalMatter.Matter;
                            MatterDetails matterDetails = originalMatter.MatterDetails;
                            Client        client        = originalMatter.Client;

                            string matterMailBody, blockUserNames;
                            // Generate Mail Subject
                            string matterMailSubject = string.Format(CultureInfo.InvariantCulture, mailSubject,
                                                                     matter.Id, matter.Name, originalMatter.MatterCreator);

                            // Step 1: Create Matter Information
                            string defaultContentType = string.Format(CultureInfo.InvariantCulture,
                                                                      defaultHtmlChunk, matter.DefaultContentType);
                            string matterType = string.Join(";", matter.ContentTypes.ToArray()).TrimEnd(';').Replace(matter.DefaultContentType, defaultContentType);
                            if (matterType == string.Empty)
                            {
                                matterType = defaultContentType;
                            }
                            // Step 2: Create Team Information
                            string secureMatter = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.SecureMatter.ToUpperInvariant() ?
                                                  ServiceConstants.NO : ServiceConstants.YES;
                            string mailBodyTeamInformation = string.Empty;
                            mailBodyTeamInformation = TeamMembersPermissionInformation(matterDetails, mailBodyTeamInformation);

                            string oneNotePath = string.Concat(client.Url, ServiceConstants.FORWARD_SLASH,
                                                               matter.MatterGuid, oneNoteLibrarySuffix,
                                                               ServiceConstants.FORWARD_SLASH, matter.MatterGuid, ServiceConstants.FORWARD_SLASH, matter.MatterGuid);

                            if (originalMatter.IsConflictCheck)
                            {
                                string conflictIdentified = ServiceConstants.FALSE.ToUpperInvariant() == matter.Conflict.Identified.ToUpperInvariant() ?
                                                            ServiceConstants.NO : ServiceConstants.YES;
                                blockUserNames = string.Join(";", matter.BlockUserNames.ToArray()).Trim().TrimEnd(';');

                                blockUserNames = !String.IsNullOrEmpty(blockUserNames) ? string.Format(CultureInfo.InvariantCulture,
                                                                                                       "<div>{0}: {1}</div>", "Conflicted User", blockUserNames) : string.Empty;
                                matterMailBody = string.Format(CultureInfo.InvariantCulture,
                                                               matterMailBodyMatterInformation, client.Name, client.Id,
                                                               matter.Name, matter.Id, matter.Description, matterType) + string.Format(CultureInfo.InvariantCulture,
                                                                                                                                       matterMailBodyConflictCheck, ServiceConstants.YES, matter.Conflict.CheckBy,
                                                                                                                                       Convert.ToDateTime(matter.Conflict.CheckOn, CultureInfo.InvariantCulture).ToString(matterCenterDateFormat, CultureInfo.InvariantCulture),
                                                                                                                                       conflictIdentified) + string.Format(CultureInfo.InvariantCulture,
                                                                                                                                                                           matterMailBodyTeamMembers, secureMatter, mailBodyTeamInformation,
                                                                                                                                                                           blockUserNames, client.Url, oneNotePath, matter.Name, originalMatter.MatterLocation, matter.Name);
                            }
                            else
                            {
                                blockUserNames = string.Empty;
                                matterMailBody = string.Format(CultureInfo.InvariantCulture, matterMailBodyMatterInformation,
                                                               client.Name, client.Id, matter.Name, matter.Id,
                                                               matter.Description, matterType) + string.Format(CultureInfo.InvariantCulture, matterMailBodyTeamMembers, secureMatter,
                                                                                                               mailBodyTeamInformation, blockUserNames, client.Url, oneNotePath, matter.Name, originalMatter.MatterLocation, matter.Name);
                            }

                            EmailMessage email = new EmailMessage(service);
                            foreach (IList <string> userNames  in matter.AssignUserEmails)
                            {
                                foreach (string userName in userNames)
                                {
                                    if (!string.IsNullOrWhiteSpace(userName))
                                    {
                                        using (var ctx = new ClientContext(originalMatter.Client.Url))
                                        {
                                            SecureString password = Utility.GetEncryptedPassword(configuration["General:AdminPassword"]);
                                            ctx.Credentials = new SharePointOnlineCredentials(configuration["General:AdminUserName"], password);
                                            if (CheckUserPresentInMatterCenter(ctx, originalMatter.Client.Url, userName, null, log))
                                            {
                                                email.ToRecipients.Add(userName);
                                            }
                                        }
                                    }
                                }
                            }
                            email.From    = new EmailAddress(adminUserName);
                            email.Subject = matterMailSubject;
                            email.Body    = matterMailBody;
                            email.Send();
                            Utility.UpdateTableStorageEntity(originalMatter, log, configuration["Data:DefaultConnection:AzureStorageConnectionString"],
                                                             configuration["Settings:MatterRequests"], "Accepted");
                        }
                    }
                }
            }
        }